First Person Camera

First person mouse look camera with sensitivity, vertical clamp, and cursor lock. Plug into any FPS or exploration game.

File
FirstPersonCamera.cs
Unity
2022.3+
Level
beginner
Size
2.4 KB · 80 lines
Added
2026-03-02
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

A Pro version adds more: First Person Camera Pro.

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 5 serialized fields with their defaults.

Inspector
Script FirstPersonCamera
Sensitivity
Mouse Sensitivity 2
Smoothing 1.5
Vertical Clamp
Min Vertical Angle -90
Max Vertical Angle 90
Cursor
Lock Cursor On Start

When to use it

Essential for any first-person game — FPS shooters, horror games, walking simulators, and exploration titles. Use this when you need standard mouse-look controls on a camera that is a child of the player object. The AddRecoil method makes it especially suited for FPS games with weapon systems.

Setup

  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

Features

  • Smooth mouse look with SmoothDamp for configurable input smoothing
  • Vertical angle clamping to prevent over-rotation
  • Automatic cursor lock on start with Escape toggle
  • Separate horizontal (player body) and vertical (camera) rotation
  • AddRecoil method for weapon kickback integration
  • Pauses rotation when cursor is unlocked for menu interaction

Inspector fields

Every field First Person Camera exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Mouse SensitivitymouseSensitivityfloat2Sensitivity
Smoothingsmoothingfloat1.5Sensitivity
Min Vertical AngleminVerticalAnglefloat-90Vertical Clamp
Max Vertical AnglemaxVerticalAnglefloat90Vertical Clamp
Lock Cursor On StartlockCursorOnStartbooltrueCursor

Public API

What other scripts can call on FirstPersonCamera.

MemberReturnsDescription
AddRecoil(float vertical, float horizontal)voidAdd recoil to the camera (e.g., from weapon fire).

Requirements

  • Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.

Common mistakes

The camera must be a child of the player GameObject — the script uses transform.parent to find the player body for horizontal rotation. If the camera is not parented correctly, horizontal rotation won't work. Setting smoothing too low makes the camera feel sluggish, while too high makes it jittery. The minVerticalAngle and maxVerticalAngle are in degrees, not radians.

Source code

FirstPersonCamera.cs, 80 lines, MIT licensed. Paste it into a new C# file of the same name.

FirstPersonCamera.cs
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;
    }
}

Next step

First Person Camera ProAdvanced FPS camera with head bob, weapon sway, lean, recoil system, and FOV zoom for immersive first person gameplay.