123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class MoveCamera : MonoBehaviour
- {
- private Camera cam;
-
- // Rotation variables
- [SerializeField]
- private float rotationSpeed = 5f; //How sensitive it with mouse
- [SerializeField]
- private float maxVerticalRotation = 45.0f;
- private float rotationX = 0.0f;
- private float rotationY = 0.0f;
- // Zoom variables
- //[SerializeField]
- //private float zoomSpeed = 20f; //How sensitive it with mousewheel
- //private float minFov = 10f;
- //private float maxFov = 90f;
- // Scroll variables (Go up, down in World coordinates)
- [SerializeField]
- private float scrollSpeed = 20f;
- // Move variables
- [SerializeField]
- private float mainSpeed = 7.0f; //regular speed
- private float shiftAdd = 5.0f; //multiplied by how long shift is held. Basically running
- private float maxShift = 27.0f; //Maximum speed when holding shift
- private float totalRun = 1.0f;
- // Reset variables
- private Vector3 startPos;
- private Quaternion startRot;
- private void Start()
- {
- cam = GameObject.Find("MoveCamera").GetComponent<Camera>();
- startPos = cam.transform.position;
- startRot = cam.transform.rotation;
- rotationX = transform.eulerAngles.x;
- rotationY = transform.eulerAngles.y;
- }
- void Update()
- {
- if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.Escape))
- {
- cam.transform.position = startPos;
- cam.transform.rotation = startRot;
- }
- // Mouse camera angle
- if (Input.GetMouseButton(0))
- {
- // Blocks rotating camera if the playback is used
- if (EventSystem.current.currentSelectedGameObject != null)
- return;
- // Clamps the vertical rotation to maxVrticalRotation
- rotationY += rotationSpeed * Input.GetAxis("Mouse X");
- rotationX -= rotationSpeed * Input.GetAxis("Mouse Y");
- rotationX = Mathf.Clamp(rotationX, -maxVerticalRotation, maxVerticalRotation);
- transform.eulerAngles = new Vector3(rotationX, rotationY, 0);
- }
- // MouseWheel Zoom In Out
- //float fov = cam.fieldOfView;
- //fov -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
- //fov = Mathf.Clamp(fov, minFov, maxFov);
- //cam.fieldOfView = fov;
- // MouseWheel up and down
- float scroll = Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
- transform.Translate(new Vector3(0, scroll, 0), Space.World);
- // Keyboard commands camera movement
- Vector3 p = GetBaseInput();
- if (p.sqrMagnitude > 0)
- {
- // only move while a direction key is pressed
- if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
- {
- totalRun += Time.deltaTime;
- p = p * totalRun * shiftAdd;
- p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
- p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
- p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
- }
- else
- {
- totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
- p = p * mainSpeed;
- }
- p = p * Time.deltaTime;
- Vector3 newPosition = transform.position;
- if (Input.GetKey(KeyCode.Space))
- {
- transform.Translate(p);
- }
- else
- {
- //If player wants to move on X and Z axis only
- transform.Translate(p);
- newPosition.x = transform.position.x;
- newPosition.z = transform.position.z;
- transform.position = newPosition;
- }
- }
- }
- private Vector3 GetBaseInput()
- { //returns the basic values, if it's 0 than it's not active.
- Vector3 p_Velocity = new Vector3();
- if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
- {
- p_Velocity += new Vector3(0, 0, 1);
- }
- if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
- {
- p_Velocity += new Vector3(0, 0, -1);
- }
- if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
- {
- p_Velocity += new Vector3(-1, 0, 0);
- }
- if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
- {
- p_Velocity += new Vector3(1, 0, 0);
- }
- return p_Velocity;
- }
- }
|