🎮

First-Person Player Setup

Nov 26, 2025

Overview

The tutorial sets up a basic first-person player in Unity for a 3D survival game, covering scene setup, camera, character controller, mouse look, movement, jumping, and ground detection.

Project Setup

  • Target audience: users with prior Unity and coding experience; fast-paced series.
  • Create a new 3D project named “3D Survival game” (any name is fine).
  • Goal: implement a first-person player with movement and camera look.

Scene Preparation

  • Add Terrain: 3D Object > Terrain; leave default settings initially.
  • Create Player: empty GameObject named “player”; position in front of the scene view.
  • Add a Cylinder as a child: scale wider and taller; center within the parent.
  • Remove Cylinder Capsule Collider: Character Controller will handle collisions.
  • Add Character Controller to player: adjust radius to match cylinder (~0.57–0.6).

Camera Setup (First-Person)

  • Drag Main Camera into player GameObject as a child.
  • Reset camera local position; move to head height at top of the cylinder.
  • First-person view allows immersive look from the player’s eyes.

Scripts and Attachment

  • Create folder: Assets/Scripts.
  • Create two C# scripts: MouseMovement, PlayerMovement.
  • Paste provided code contents into each class (inside class brackets).
  • Attach both scripts to the player object in the Inspector.

Mouse Look (MouseMovement script)

  • Purpose: rotate view with mouse; lock/hide cursor during play.
  • Public settings:
    • mouseSensitivity default 100; adjust in Inspector as needed.
  • Key variables:
    • xRotation, yRotation track rotation degrees around axes.
  • Start:
    • Cursor.lockState = CursorLockMode.Locked to center and hide cursor.
  • Update:
    • Read “Mouse X” and “Mouse Y” inputs from Input Manager.
    • Scale by mouseSensitivity and Time.deltaTime for frame-rate independence.
    • xRotation minus-equals mouseY to invert vertical look (natural FPS behavior).
    • yRotation plus-equals mouseX for horizontal look.
    • Clamp xRotation to [-90, 90] to prevent over-rotation (no “owl neck”).
    • Apply rotation using Quaternion.Euler(xRotation, yRotation, 0) to the player transform.

Player Movement and Jump (PlayerMovement script)

  • Public settings (defaults):
    • speed = 12; gravity = -9.81f * 2; jumpHeight = 3.
    • stepOffset and slope handled by Character Controller settings; e.g., step offset ~0.7.
  • References:
    • controller: assign the player’s CharacterController component.
    • groundCheck: create empty child “ground check”; place at player’s feet.
    • groundDistance: ~0.4; radius for sphere ground detection.
    • groundMask: LayerMask used to identify ground layers.*

Ground Detection

  • Physics.CheckSphere(groundCheck.position, groundDistance, groundMask) determines isGrounded.
  • On grounding and downward velocity, reset vertical velocity to a small negative (e.g., -2) to avoid accumulating fall speed.

Movement Input and Application

  • Read axes:
    • Horizontal: A/D or Left/Right; Vertical: W/S or Up/Down.
  • Build movement vector:
    • move = transform.right * x + transform.forward * z (XZ-plane).
  • Move:
    • controller.Move(move * speed * Time.deltaTime) for smooth, frame-independent motion.

Jumping

  • Condition: if Input.GetButtonDown("Jump") and isGrounded, then jump.
  • Set vertical velocity using the physics-based formula:
    • velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity).
  • Apply gravity each frame:
    • velocity.y += gravity * Time.deltaTime.
  • Apply vertical motion:
    • controller.Move(velocity * Time.deltaTime).

Layers and Ground Mask Configuration

  • Add a Layer named “Ground”.
  • Assign Terrain (and any platforms/stairs) to the Ground layer.
  • Set PlayerMovement.groundMask to Ground in the Inspector.
  • Without layer assignment, ground checks will fail and jumping will not work.

Practical Notes and Tuning

  • Sensitivity: increase (e.g., 200) if look is too slow; platform-dependent feel.
  • Gravity feel: adjust gravity multiplier if fall is too floaty or too snappy.
  • Clamp only vertical look; do not clamp horizontal rotation for a human player.
  • Step Offset and Slope Limit: tune to traverse stairs and hills without jumping.

Demonstrated Results

  • Player can:
    • Look around with mouse, clamped vertically.
    • Move with WASD/arrow keys.
    • Jump with Space when grounded; lands under gravity.

Settings Summary

SettingLocationDefault / ExamplePurpose
mouseSensitivityMouseMovement (Inspector)100–200Controls look speed
xRotation/yRotationMouseMovement (private)0Tracks current rotation
speedPlayerMovement (Inspector)12Movement speed
gravityPlayerMovement (Inspector)-9.81 * 2Downward acceleration
jumpHeightPlayerMovement (Inspector)3Jump strength
groundCheckPlayerMovement (Transform)Child at feetGround sphere origin
groundDistancePlayerMovement (Inspector)~0.4Sphere radius for ground
groundMaskPlayerMovement (LayerMask)GroundIdentifies ground layers
controllerPlayerMovement (Component)CharacterControllerMovement and collision
Cursor LockMouseMovement StartLockedHide and center cursor

Input Mappings (Unity Input Manager)

ActionAxis/ButtonDefault KeysDescription
Look HorizontalMouse XMouse move XYaw rotation
Look VerticalMouse YMouse move YPitch rotation
Move HorizontalHorizontalA/D, Left/RightStrafe left/right
Move VerticalVerticalW/S, Up/DownMove forward/back
JumpJumpSpaceInitiate jump

Key Terms & Definitions

  • Character Controller: Unity component providing capsule-based movement with collisions.
  • Ground Check: Transform at feet used as the center for Physics.CheckSphere ground detection.
  • LayerMask: Bitmask selecting layers to include in physics queries.
  • Quaternion.Euler: Constructs a rotation from Euler angles (degrees) for X, Y, Z axes.
  • Time.deltaTime: Time between frames; used for frame-rate independent motion.
  • Clamp: Restrict a value within a min and max range.

Action Items / Next Steps

  • Assign Ground layer to Terrain and any walkable surfaces; set groundMask accordingly.
  • Tune mouseSensitivity, speed, gravity, jumpHeight for preferred feel.
  • Verify groundCheck position at player’s feet; confirm groundDistance radius detects contact.
  • Future additions: implement running, refine movement, build environment with terrain, trees, and grass.