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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

You won’t want to leave your home office ever again

Migrating to AWS Сloud: what’s in it for you and how do you do it right?

Dual boot Kali Linux 2020 with Windows 10 on a laptop with Nvidia Graphics for use as a daily…

Best Way to Factory Reset Locked iPad without Passcode Easily

Best Way to Factory Reset Locked iPad without Passcode Easily Featured Image

Ultra Easy Python automated virtual currency FX backtesting (using BitMEX REST API)

Introducing MACH composer

LeetCode — Minimum Window Substring

Get development started with Sitecore Part 4: A Better Way

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
Niklas Bergstrand

Niklas Bergstrand

Unity / C# Game developer

More from Medium

We are pleased to announce a partnership agreement with the Enrex project

Emily Dickinson’s Poems

Coachella Created a New Scoreboard

Jack & “fox” get me fired up. (No disrespect Jack.)