Advertisement
Guest User

Untitled

a guest
Mar 18th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using Cinemachine;
  2. using UnityEngine;
  3.  
  4. public class PlayerCamera : MonoBehaviour
  5. {
  6.     [Header("References")]
  7.     [Tooltip("Player input")]
  8.     [SerializeField] private Inputs _inputs;
  9.  
  10.     [Header("Camera")]
  11.     [Tooltip("Camera sensitivity")]
  12.     [SerializeField] private float _sensitivity = 1.0f;
  13.     [Tooltip("Additional degress to override the camera. Useful for fine tuning camera position when locked")]
  14.     [SerializeField] private float _cameraAngleOverride = 0.0f;
  15.  
  16.     [Header("Cinemachine")]
  17.     [Tooltip("Get a reference to our Cinemachine Virtual Camera")]
  18.     [SerializeField] private CinemachineVirtualCamera _cinemachineVirtualCamera;
  19.     private Cinemachine3rdPersonFollow _cinemachineFollow;
  20.     [Tooltip("The follow target set in the Cinemachine Virtual Camera that the camera will follow")]
  21.     [SerializeField] private GameObject _cinemachineCameraTarget;
  22.  
  23.     [Header("Zoom")]
  24.     [Tooltip("Max value for zoom in")]
  25.     [SerializeField] private float _zoomInMax = 10f;
  26.     [Tooltip("Max value for zoom out")]
  27.     [SerializeField] private float _zoomOutMax = 30f;
  28.  
  29.     // Variables
  30.     private const float _threshold = 0.01f;
  31.     private float _cinemachineTargetYaw;
  32.     private float _cinemachineTargetPitch = 45f;
  33.     private float _cameraTargetDistance;
  34.  
  35.     private void Awake()
  36.     {
  37.         _cinemachineFollow = _cinemachineVirtualCamera.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
  38.     }
  39.  
  40.     private void Start()
  41.     {
  42.         _cameraTargetDistance = _cinemachineFollow.CameraDistance;
  43.     }
  44.  
  45.     private void LateUpdate()
  46.     {
  47.         CameraRotation();
  48.         CameraZoom();
  49.     }
  50.  
  51.     private void CameraRotation()
  52.     {
  53.         // If there is an input and camera position is not fixed
  54.         if (_inputs.look.sqrMagnitude >= _threshold && !_inputs.lockCamera)
  55.         {
  56.             _cinemachineTargetYaw += _inputs.look.x * Time.deltaTime * _sensitivity;
  57.             //_cinemachineTargetPitch += _inputs.look.y * Time.deltaTime * _sensitivity;
  58.  
  59.             //float rotateDir = 0f;      
  60.             //if (_inputs.look.x > 0f)
  61.             //{
  62.             //    rotateDir = 1f;
  63.             //}
  64.             //else if (_inputs.look.x < 0f)
  65.             //{
  66.             //    rotateDir = -1f;
  67.             //}
  68.             //float rotateSpeed = 300f;
  69.             //_cinemachineTargetYaw += rotateDir * rotateSpeed * Time.deltaTime * _sensitivity;
  70.         }
  71.  
  72.         // Clamp our rotations so our values are limited 360 degrees
  73.         _cinemachineTargetYaw = ClampAngle(_cinemachineTargetYaw, float.MinValue, float.MaxValue);
  74.         //_cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);
  75.  
  76.         // Cinemachine will follow this target
  77.         _cinemachineCameraTarget.transform.rotation = Quaternion.Euler(_cinemachineTargetPitch + _cameraAngleOverride, _cinemachineTargetYaw, 0.0f);
  78.     }
  79.  
  80.     private void CameraZoom()
  81.     {
  82.         if (_inputs.zoom > 0f)
  83.         {
  84.             _cameraTargetDistance += 5f;
  85.         }
  86.         if (_inputs.zoom < 0f)
  87.         {
  88.             _cameraTargetDistance -= 5f;
  89.         }
  90.         //_inputs.zoom = 0f;
  91.         _cameraTargetDistance = Mathf.Clamp(_cameraTargetDistance, _zoomInMax, _zoomOutMax);
  92.         float zoomSpeed = 5f;
  93.         _cinemachineFollow.CameraDistance = Mathf.Lerp(_cinemachineFollow.CameraDistance, _cameraTargetDistance, Time.deltaTime * zoomSpeed);
  94.     }
  95.  
  96.     private static float ClampAngle(float lfAngle, float lfMin, float lfMax)
  97.     {
  98.         if (lfAngle < -360f) lfAngle += 360f;
  99.         if (lfAngle > 360f) lfAngle -= 360f;
  100.         return Mathf.Clamp(lfAngle, lfMin, lfMax);
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement