using System.Collections; using System.Collections.Generic; using UnityEngine; using static UnityEditor.PlayerSettings; public class CameraMovement : MonoBehaviour { [Header("Speeds")] public float MovementSpeed = 50.0f; public float RotationSpeed = 60.0f; public float TiltSpeed = 30f; public float ZoomSpeed = 1.0f; [Header("Bounds")] public Vector3 CameraMinimumBound = new Vector3(-100, 1, -250); public Vector3 CameraMaximumBound = new Vector3(100, 25, 250); public float EdgeScrollThreshold = 5.0f; public float MinFOV = 25f; public float MaxFOV = 100f; public float MinTilt = -75f; public float MaxTilt = 75f; private bool hasElevationAxis = true; private bool hasRotationAxis = true; private bool hasTiltAxis = true; // Start is called before the first frame update private Camera cam; private float tiltAngle; void Start() { cam = GetComponent(); tiltAngle = this.transform.localEulerAngles.x; if(cam == null) { Debug.Log("Attach the CameraMovement script to your scene's main Camera object."); } // Has the Elevation axis been created? try { Input.GetAxis("Elevation"); } catch { Debug.Log("Elevation axis is missing"); hasElevationAxis = false; } // has the Rotation try { Input.GetAxis("Rotation"); } catch { Debug.Log("Rotation axis is missing"); hasRotationAxis = false; } try { Input.GetAxis("Tilt"); } catch { Debug.Log("Tilt axis is missing"); hasTiltAxis = false; } } // Update is called once per frame void Update() { if(cam != null) { KeyboardMovement(); MouseMovement(); } } private void MouseMovement() { if(Input.GetMouseButton(2)) // Middle mouse (scroll wheel) held down { float mouseY = Input.GetAxis("Mouse Y"); tiltAngle -= mouseY * TiltSpeed * Time.deltaTime; tiltAngle = Mathf.Clamp(tiltAngle, MinTilt, MaxTilt); transform.localRotation = Quaternion.Euler(tiltAngle, this.transform.localEulerAngles.y, 0f); return; } // Move camera when mouse is near the screen edge Vector3 currentPosition = this.transform.position; Vector3 mouse = Input.mousePosition; if(mouse.x >= Screen.width - EdgeScrollThreshold) currentPosition.x -= MovementSpeed * Time.deltaTime; if(mouse.x <= EdgeScrollThreshold) currentPosition.x += MovementSpeed * Time.deltaTime; if(mouse.y >= Screen.height - EdgeScrollThreshold) currentPosition.z -= MovementSpeed * Time.deltaTime; // assuming top-down scroll on Z if(mouse.y <= EdgeScrollThreshold) currentPosition.z += MovementSpeed * Time.deltaTime; this.transform.position = currentPosition; // Zoom camera by changing its focal length float zoomAmount = Input.mouseScrollDelta.y * ZoomSpeed * Time.deltaTime; cam.fieldOfView -= zoomAmount; cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, MinFOV, MaxFOV); // Tilt camera when middle mouse is depressed } private void KeyboardMovement() { // Move the camerar Vector3 movement = new Vector3(0, 0, 0); movement.x -= Input.GetAxis("Horizontal"); movement.z -= Input.GetAxis("Vertical"); if(hasElevationAxis) movement.y += Input.GetAxis("Elevation"); movement *= MovementSpeed; this.transform.Translate(movement * Time.deltaTime, Space.World); // Clamp camera position within bounds Vector3 currentPosition = this.transform.position; currentPosition.x = Mathf.Clamp(currentPosition.x, CameraMinimumBound.x, CameraMaximumBound.x); currentPosition.y = Mathf.Clamp(currentPosition.y, CameraMinimumBound.y, CameraMaximumBound.y); currentPosition.z = Mathf.Clamp(currentPosition.z, CameraMinimumBound.z, CameraMaximumBound.z); this.transform.position = currentPosition; // Rotate camera left/right (relative to world) Vector3 rotation = new Vector3(0, 0, 0); if(hasRotationAxis) rotation.y = Input.GetAxis("Rotation"); rotation = rotation * RotationSpeed; this.transform.Rotate(rotation * Time.deltaTime, Space.World); // Tilt camera up/down (relative to self) if(hasTiltAxis) tiltAngle = tiltAngle - Input.GetAxis("Tilt") * TiltSpeed * Time.deltaTime; tiltAngle = Mathf.Clamp(tiltAngle, MinTilt, MaxTilt); this.transform.rotation = Quaternion.Euler(tiltAngle, this.transform.localEulerAngles.y, 0); } }