Last active
September 5, 2024 03:30
-
-
Save pppoe252110/19e844ee57f2750fa25e1faf47060cc3 to your computer and use it in GitHub Desktop.
Unity First Person Camera Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class CameraController : MonoBehaviour | |
{ | |
[SerializeField] private float _sensitivity = 3; | |
[SerializeField] private float _maxAngle = 80; | |
[SerializeField] private float _minAngle = -80; | |
[SerializeField] private Transform _horizontalRotation; | |
[SerializeField] private Transform _verticalRotation; | |
private Vector2 currentRotation; | |
void Update() | |
{ | |
currentRotation = new Vector2(currentRotation.x + Input.GetAxis("Mouse X") * _sensitivity, Mathf.Clamp(currentRotation.y + Input.GetAxis("Mouse Y") * _sensitivity, _minAngle, _maxAngle)); | |
_horizontalRotation.localRotation = Quaternion.Euler(_horizontalRotation.localEulerAngles.x, currentRotation.x, _horizontalRotation.localEulerAngles.z); | |
_verticalRotation.localRotation = Quaternion.Euler(-currentRotation.y, _verticalRotation.localEulerAngles.y, _verticalRotation.localEulerAngles.z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment