Simple first-person controls in Unity

Let’s go over how to set up simple first-person controls

First, setup the environment and add a capsule for the player:

Add the main camera as a child on the player object and position it:

On the player object remove the capsule collider and disable the mesh renderer. Add a CharacterController to the player object:

Create a script called PlayerMovement and attach to the player object:

Open the player script and add:
-float for mouse sensitivity
-float for the movement speed of the player
-float for how high the player will jump
-float for gravity
-float to control the vertical rotation
-float for max vertical rotation angle
-three floats for player velocity
-CharacterController for the CC on the player

Initialise the CharacterController in the start function. To prevent the mouse cursor from showing set Cursor.lockstate to CursorLockMode.Locked:

Create a function for toggling the mouse cursor:

Create the movement function. Clamp the direction to ensure the player does not move quicker if moving diagonally. Convert the movement from global space to local space by passing in direction to transform.TransformDirection. Without coverting the to local space the player will not move based on its rotation:

Create the rotation function.The vertical rotation will only affect the main camera, if not the whole player object would be spinning around when looking up an down. Clamp the vertical rotation by 45 degrees:

Lastly, call the the functions from the update function:

Good luck!



Unity / C# Game developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store