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

HomeGuy3: Air Conditioning Contractors Can Help You Save https://t.co/aIaWLftAQL #a

How to Anonymise Places in Python

PING TO SINGLE COMPUTER USING ROTE TABLE

How to give aliases to SSH user and IP addresses

Day 6 and 7 — Ending the controllers and implement a CI-CD

Introduction to FPGA and its Architecture

AZ-204 Exam: The Most Suitable Option for Cloud Developers

Direct Cache Mapping and Memory Access Time

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

Last Mile Education Fund Boosts North Texas Biotech and STEM Students: College Students in North…

My Experience Walking in Worsham Canyon

Reading Notes — Grit: the Power of Passion and Perseverance

“Boys Will Be Boys”