diff --git a/Assets/Scenes/CameraSimon.unity b/Assets/Scenes/CameraSimon.unity new file mode 100644 index 0000000..fdf50a2 Binary files /dev/null and b/Assets/Scenes/CameraSimon.unity differ diff --git a/Assets/Scenes/CameraSimon.unity.meta b/Assets/Scenes/CameraSimon.unity.meta new file mode 100644 index 0000000..3a63c3a --- /dev/null +++ b/Assets/Scenes/CameraSimon.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 335c19c80d0e1cd4abcff9c519259b11 +timeCreated: 1440294415 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs new file mode 100644 index 0000000..bb57ce4 --- /dev/null +++ b/Assets/Scripts/CameraController.cs @@ -0,0 +1,182 @@ +using UnityEngine; +using System.Collections.Generic; + +[RequireComponent(typeof(Camera))] +public class CameraController : MonoBehaviour +{ + private List players; + private Camera cam; + private float nextUpdate = 0f; + private const float UpdateTime = 0.1f; + private bool movingLeft = false; + private bool movingRight = false; + private bool movingUp = false; + private bool movingDown = false; + private bool zoomingIn = false; + private bool zoomingOut = false; + + private bool debug = false; + + private float XOffset = 0.25f; + private float YOffset = 0.15f; + + private Vector3 upMove; + + // Use this for initialization + void Start () + { + cam = GetComponent(); + players = new List(); + + GameObject[] gos = GameObject.FindGameObjectsWithTag("Player"); + foreach (GameObject go in gos) + { + if (!go.Equals(gameObject)) + { + players.Add(go.transform); + } + } + + upMove = new Vector3(transform.forward.x, 0, transform.forward.z).normalized; + } + + // Update is called once per frame + void Update () + { + nextUpdate -= Time.deltaTime; + if (nextUpdate < 0) + { + CalculateCameraMovement(); + nextUpdate += UpdateTime; + } + + if (Input.GetKeyDown(KeyCode.Z)) + debug = true; + + if (movingUp) + transform.Translate(upMove * -Time.deltaTime, Space.World); + + if (movingDown) + transform.Translate(upMove * Time.deltaTime, Space.World); + + if (movingLeft) + transform.Translate(-Time.deltaTime, 0, 0, Space.Self); + + if (movingRight) + transform.Translate(Time.deltaTime, 0, 0, Space.Self); + + int mod = 0; + if (zoomingIn) + mod = -10; + if (zoomingOut) + mod = 10; + + //cam.fieldOfView = Mathf.Clamp(cam.fieldOfView + mod * Time.deltaTime, 30, 60); + } + + void CalculateCameraMovement() + { + List screenPos = GetScreenPositions(); + + float minX = 2, maxX = -2, minY = 2, maxY = -2; + foreach (Vector3 pos in screenPos) + { + if (pos.x > maxX) + maxX = pos.x; + if (pos.x < minX) + minX = pos.x; + + if (pos.y > maxY) + maxY = pos.y; + if (pos.y < minY) + minY = pos.y; + } + + float distX = maxX - minX; + float distY = maxY - minY; + + if (debug) + { + Debug.Log("Dist X - " + distX + "\nDist Y - " + distY); + debug = false; + } + + if (movingLeft) + { + if(minX > 0.5f - XOffset / 2f) + movingLeft = false; + } + else + { + if(minX < 0.5f - XOffset) + movingLeft = true; + } + + if (movingRight) + { + if(maxX < 0.5f + XOffset / 2f) + movingRight = false; + } + else + { + if(maxX > 0.5f + XOffset) + movingRight = true; + } + + if (movingUp) + { + if (minY > 0.5f - YOffset / 2f) + movingUp = false; + } + else + { + if (minY < 0.5f - YOffset) + movingUp = true; + } + + if (movingDown) + { + if (maxY < 0.5f + YOffset / 2f) + movingDown = false; + } + else + { + if (maxY > 0.5f + YOffset) + movingDown = true; + } + + if (zoomingOut) + { + if (distX < 0.65f) + //if (Mathf.Min(distX, distY) < 0.8f) + zoomingOut = false; + } + else + { + if (distX > 0.75f) + //if (Mathf.Max(distX, distY) > 0.9f) + zoomingOut = true; + } + + if (zoomingIn) + { + if (distX > 0.7f) + //if (Mathf.Max(distX, distY) > 0.7f) + zoomingIn = false; + } + else + { + if (distX < 0.6f) + //if (Mathf.Min(distX, distY) < 0.6f) + zoomingIn = true; + } + } + + private List GetScreenPositions() + { + List list = new List(); + foreach (Transform t in players) + list.Add(cam.WorldToViewportPoint(t.position)); + return list; + } +} diff --git a/Assets/Scripts/CameraController.cs.meta b/Assets/Scripts/CameraController.cs.meta new file mode 100644 index 0000000..252721c --- /dev/null +++ b/Assets/Scripts/CameraController.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 88e55981fb5053743beab34a961a19e6 +timeCreated: 1440294816 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: