Part of these game systems:
First Person Camera
First person mouse look camera with sensitivity, vertical clamp, and cursor lock. Plug into any FPS or exploration game.
How to Use
1
Create your Player object with a child Camera
2
Attach FirstPersonCamera to the Camera
3
Adjust mouse sensitivity and vertical clamp angles
4
Cursor locks automatically on Start
5
Press Escape to toggle cursor lock
6
Pair with 3D Player Controller for complete FPS movement
Source Code
C#
using UnityEngine;
/// <summary>
/// First person mouse look camera. Attach to the Camera (child of player).
/// Handles horizontal rotation on the player body and vertical rotation on the camera.
/// </summary>
public class FirstPersonCamera : MonoBehaviour
{
[Header("Sensitivity")]
[SerializeField] private float mouseSensitivity = 2f;
[SerializeField] private float smoothing = 1.5f;
[Header("Vertical Clamp")]
[SerializeField] private float minVerticalAngle = -90f;
[SerializeField] private float maxVerticalAngle = 90f;
[Header("Cursor")]
[SerializeField] private bool lockCursorOnStart = true;
private Transform playerBody;
private float xRotation;
private float yRotation;
private Vector2 currentMouseDelta;
private Vector2 currentMouseDeltaVelocity;
private void Awake()
{
playerBody = transform.parent;
}
private void Start()
{
if (lockCursorOnStart)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
bool locked = Cursor.lockState == CursorLockMode.Locked;
Cursor.lockState = locked ? CursorLockMode.None : CursorLockMode.Locked;
Cursor.visible = locked;
}
if (Cursor.lockState != CursorLockMode.Locked) return;
Vector2 targetMouseDelta = new Vector2(
Input.GetAxisRaw("Mouse X"),
Input.GetAxisRaw("Mouse Y")
);
currentMouseDelta = Vector2.SmoothDamp(
currentMouseDelta, targetMouseDelta,
ref currentMouseDeltaVelocity, 1f / smoothing
);
xRotation -= currentMouseDelta.y * mouseSensitivity;
xRotation = Mathf.Clamp(xRotation, minVerticalAngle, maxVerticalAngle);
yRotation += currentMouseDelta.x * mouseSensitivity;
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
if (playerBody != null)
playerBody.rotation = Quaternion.Euler(0f, yRotation, 0f);
}
/// <summary>
/// Add recoil to the camera (e.g., from weapon fire).
/// </summary>
public void AddRecoil(float vertical, float horizontal)
{
xRotation -= vertical;
yRotation += horizontal;
}
}