using System.Collections; using System.Collections.Generic; using UnityEngine; public class CarCameraMovement : MonoBehaviour { public float _mouseSensitivity = 3.5f; //How quick is the sensitivity of the mouse . [HideInInspector] public float _mouseSensitivityDefault; float xRotation = 0f; float yRotation = 0f; private static CameraMovement _instance; public static CameraMovement GetInstance() { return _instance; } float mouseX; float mouseY; private Camera _camera; public float cameraFieldOfViewDefault; public float lerpFieldOfView; public bool IsZooming; public bool CanLerpValue = true; // Start is called before the first frame update void Start() { Cursor.lockState = CursorLockMode.Locked; //locks the cursors position to the center of the screen. Cursor.visible = false; _mouseSensitivityDefault = _mouseSensitivity; _camera = GetComponent(); cameraFieldOfViewDefault = _camera.fieldOfView; } // Update is called once per frame void Update() { CameraMovement(); } public void CameraMovement() { mouseX = Input.GetAxis("Mouse X") * _mouseSensitivity; mouseY = Input.GetAxis("Mouse Y") * _mouseSensitivity; xRotation -= mouseY; xRotation = Mathf.Clamp(xRotation, -30f, 30f); // How much the camera can turn in the y axis. transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f); yRotation += mouseX; yRotation = Mathf.Clamp(yRotation, -30f, 30f); if (Input.GetKey(KeyCode.Mouse1)) { if (CanLerpValue) { lerpFieldOfView = 0f; IsZooming = true; StopAllCoroutines(); StartCoroutine(ZoomInFieldOfView()); CanLerpValue = false; } } else { if (IsZooming) { CanLerpValue = true; lerpFieldOfView = 0f; StopAllCoroutines(); StartCoroutine(ZoomOutFieldOfView()); IsZooming = false; } } } public IEnumerator ZoomInFieldOfView() { while (lerpFieldOfView < 1f) { lerpFieldOfView += 0.5f * Time.deltaTime; _camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 45f, lerpFieldOfView); print("Is zooming in"); yield return null; } } public IEnumerator ZoomOutFieldOfView() { while (lerpFieldOfView < 1f) { lerpFieldOfView += 0.5f * Time.deltaTime; _camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, cameraFieldOfViewDefault, lerpFieldOfView); print("Is zooming out"); yield return null; } } }