Advertisement
ImAxel0

CameraController

Jun 12th, 2025
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | Gaming | 0 0
  1. public class CameraController : MonoBehaviour
  2. {
  3.     private Camera _camera;
  4.     private float _defaultFov;
  5.     private float _currentFov;
  6.     private float _currentTilt;
  7.     [SerializeField] private float _fovChangeOnRun = 10f;
  8.     [SerializeField] private float _fovLerpSpeed = 2f;
  9.     [SerializeField] private float _fovLerpReturn = 10f;
  10.     [SerializeField] private float _tiltLerpSpeed = 4f;
  11.     [SerializeField] private float _tiltLerpReturn = 10f;
  12.     [SerializeField] private float _tiltAmount = 1f;
  13.     [SerializeField] float _bobSpeed = 14f;
  14.     [SerializeField] float _bobAmount = 0.02f;
  15.  
  16.     private Vector2 _rawLookInput;
  17.     private Vector2 _lookInputReady;
  18.  
  19.     [SerializeField] private float _horizSensitity;
  20.     [SerializeField] private float _verticalSensitity;
  21.     [SerializeField] private float _sensitivityMultiplierOnForwardRun = 0.6f;
  22.  
  23.     private float _xRotation;
  24.     private float _yRotation;
  25.     private float _maxVertLookDegree = 80;
  26.  
  27.     private void Start()
  28.     {
  29.         _camera = GetComponent<Camera>();
  30.         _yRotation = transform.localRotation.eulerAngles.y;
  31.         _defaultFov = _camera.fieldOfView;
  32.         _currentFov = _camera.fieldOfView;
  33.         Cursor.lockState = CursorLockMode.Locked;
  34.     }
  35.  
  36.     private void Update()
  37.     {
  38.         var inputManager = FirstPersonCharacter.Instance.InputManager;
  39.  
  40.         _rawLookInput = inputManager.LookInput;
  41.         _rawLookInput.x *= _horizSensitity;
  42.         _rawLookInput.y *= _verticalSensitity;
  43.         _lookInputReady = _rawLookInput;
  44.  
  45.         // Vertical look
  46.         _xRotation -= _lookInputReady.y;
  47.         _xRotation = Mathf.Clamp(_xRotation, -_maxVertLookDegree, _maxVertLookDegree);
  48.  
  49.         // Horizontal look
  50.         _yRotation += inputManager.RunForward
  51.             ? _lookInputReady.x * _sensitivityMultiplierOnForwardRun
  52.             : _lookInputReady.x;
  53.  
  54.         float tilt = 0f;
  55.         if (inputManager.MoveOblique && inputManager.Running && inputManager.MoveInput.x > 0)
  56.         {
  57.             tilt = _tiltAmount;
  58.         }
  59.         else if (inputManager.MoveOblique && inputManager.Running && inputManager.MoveInput.x < 0)
  60.         {
  61.             tilt = -_tiltAmount;
  62.         }
  63.  
  64.         float tiltLerpSpeed = inputManager.MoveSideway
  65.             ? _tiltLerpSpeed
  66.             : _tiltLerpReturn;
  67.  
  68.         _currentTilt = Mathf.Lerp(_currentTilt, tilt, Time.deltaTime * tiltLerpSpeed);
  69.  
  70.         float lerpSpeed = inputManager.RunForward ? _fovLerpSpeed : _fovLerpReturn;
  71.         float targetFov = inputManager.RunForward
  72.             ? _defaultFov - _fovChangeOnRun
  73.             : _defaultFov;
  74.  
  75.         _currentFov = Mathf.Lerp(_currentFov, targetFov, Time.deltaTime * lerpSpeed);
  76.         _camera.fieldOfView = _currentFov;
  77.  
  78.         UpdateHeadBob(inputManager);
  79.  
  80.         FirstPersonCharacter.Instance.transform.localRotation = Quaternion.Euler(0f, _yRotation, 0f);
  81.         transform.localRotation = Quaternion.Euler(_xRotation, _yRotation, _currentTilt);
  82.     }
  83.  
  84.     private void UpdateHeadBob(PlayerInputManager inputManager)
  85.     {
  86.         if (inputManager.MoveInput != Vector2.zero)
  87.         {
  88.             float waveSlice = Mathf.Sin(Time.time * _bobSpeed);
  89.             float totalBob = waveSlice * _bobAmount;
  90.             transform.localPosition = new Vector3(
  91.                 transform.localPosition.x,
  92.                 FirstPersonCharacter.Instance.CamPositionTr.position.y + totalBob,
  93.                 transform.localPosition.z);
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement