Advertisement
illiden

Untitled

Feb 12th, 2023
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. [SerializeField] private float _speed;
  2.     [SerializeField] private float _rotationSpeedVertical;
  3.     [SerializeField] private float _rotationSpeedHorizontal;
  4.     [SerializeField] private float _minRotationX;
  5.     [SerializeField] private float _maxRotationX;
  6.     [SerializeField] private CharacterController _characterController;
  7.     [SerializeField] private Camera _camera;
  8.  
  9.     private void Update()
  10.     {
  11.         float inputX = Input.GetAxis("Horizontal");
  12.         float inputZ = Input.GetAxis("Vertical");
  13.         Vector3 direction = transform.forward * inputZ + transform.right * inputX;
  14.         _characterController.Move(direction.normalized * _speed * Time.deltaTime);
  15.  
  16.         float inputRotationY = Input.GetAxis("Mouse X");
  17.         float inputRotationX = Input.GetAxis("Mouse Y");
  18.         Vector3 rotationX = Vector3.right * inputRotationX * _rotationSpeedVertical * Time.deltaTime;
  19.         _camera.transform.Rotate(-rotationX);
  20.  
  21.         Vector3 localRotation = _camera.transform.eulerAngles;
  22.         localRotation.x = ClampAngle(localRotation.x, _minRotationX, _maxRotationX);
  23.         localRotation.z = 0;
  24.         _camera.transform.eulerAngles = localRotation;
  25.  
  26.         transform.Rotate(Vector3.up * inputRotationY * _rotationSpeedHorizontal * Time.deltaTime);
  27.     }
  28.  
  29.     private float ClampAngle(float angle, float min, float max)
  30.     {
  31.         if (angle < 0f)
  32.             angle = 360 + angle;
  33.         if (angle > 180f)
  34.             return Mathf.Max(angle, 360 + min);
  35.         return Mathf.Min(angle, max);
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement