Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SerializeField] private float _speed;
- [SerializeField] private float _rotationSpeedVertical;
- [SerializeField] private float _rotationSpeedHorizontal;
- [SerializeField] private float _minRotationX;
- [SerializeField] private float _maxRotationX;
- [SerializeField] private CharacterController _characterController;
- [SerializeField] private Camera _camera;
- private void Update()
- {
- float inputX = Input.GetAxis("Horizontal");
- float inputZ = Input.GetAxis("Vertical");
- Vector3 direction = transform.forward * inputZ + transform.right * inputX;
- _characterController.Move(direction.normalized * _speed * Time.deltaTime);
- float inputRotationY = Input.GetAxis("Mouse X");
- float inputRotationX = Input.GetAxis("Mouse Y");
- Vector3 rotationX = Vector3.right * inputRotationX * _rotationSpeedVertical * Time.deltaTime;
- _camera.transform.Rotate(-rotationX);
- Vector3 localRotation = _camera.transform.eulerAngles;
- localRotation.x = ClampAngle(localRotation.x, _minRotationX, _maxRotationX);
- localRotation.z = 0;
- _camera.transform.eulerAngles = localRotation;
- transform.Rotate(Vector3.up * inputRotationY * _rotationSpeedHorizontal * Time.deltaTime);
- }
- private float ClampAngle(float angle, float min, float max)
- {
- if (angle < 0f)
- angle = 360 + angle;
- if (angle > 180f)
- return Mathf.Max(angle, 360 + min);
- return Mathf.Min(angle, max);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement