This script provides first-person movement for a player using the CharacterController component in Unity. It includes:
โ๏ธ Basic movement (WASD)
โ๏ธ Sprinting with limited stamina
โ๏ธ Dynamic Field of View (FOV) changes when sprinting
โ๏ธ Gravity simulation
โ๏ธ Stair detection and smooth climbing
- Smooth First-Person Movement: Uses Unity's
CharacterController
for smooth motion. - Sprinting with Stamina: Sprint depletes over time and regenerates when not in use.
- Realistic Gravity: Keeps the player grounded and prevents floating.
- Stair Climbing: Detects stairs and allows smooth vertical movement.
- FOV Adjustment: Field of View increases while sprinting for a more immersive feel.
- Attach the Script: Add the
PlayerMovement
script to the player GameObject in Unity. - Add Components: Ensure the player has:
- A CharacterController component.
- A Camera attached as the player's view.
- A Ground Check empty GameObject under the player.
- Configure Variables: Adjust movement speed, sprint duration, FOV values, and gravity in the Inspector.
Action | Key |
---|---|
Move | W A S D |
Sprint | Left Shift |
Look Around | Mouse |
- Create an Empty Object under the player:
- Name it
GroundCheck
and position it at the player's feet. - Assign it to the
groundCheck
variable in the script.
- Name it
- Set up Layers:
- Add a "Ground" layer and assign it to ground objects.
- Add a "Stairs" layer and assign it to stair objects.
- Set
groundMask
to Ground andstairsMask
to Stairs in the script.
- Ensure the Player Has a CharacterController component.