Skip to content

Instantly share code, notes, and snippets.

@pppoe252110
Last active September 5, 2024 03:30
Show Gist options
  • Save pppoe252110/19e844ee57f2750fa25e1faf47060cc3 to your computer and use it in GitHub Desktop.
Save pppoe252110/19e844ee57f2750fa25e1faf47060cc3 to your computer and use it in GitHub Desktop.
Unity First Person Camera Controller
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