Simple player movement in Unity

The most basic part of the majority of games is of course the ability to control the movement of the player. Below I will go over some basic controls for a space shooter.

By default, when you create a script in Unity it will get populated with two functions Start and Update. Anything you put into the Start function will get called once when the object to which the script is attached has loaded in the scene. Anything inside the Update function will get called once per frame:

As the Update function has a tendency to end up controlling many aspects of the object to which the script is attached to, it can quickly get messy. Because of this you should stick to calling other functions from the Update function instead of the actual code:

I always try to ensure my code uses as few lines as possible to achieve the task. The first line in the player movement script takes both Horizontal and Vertical input and stores it in a struct called Vector3. The 3 in Vector3 stands for the three positions x, y and z. As the z position will not change, it is just set to 0:

Input Manager is found under Edit->Project Settings
Input Manager is found under Edit->Project Settings

But what about _speed and Time.deltaTime? Just taking in the input is not enough and you of course need to set the speed at which the player will move. This is set by the private variable _speed. As the Update function is frame dependant it will vary between devices the number of times it will get called, which is why you will need to also multiply the input by Time.deltaTime. Time.deltaTime returns the average time between frames ensuring the game will run the same on any device.

Fun fact: The famous game Space Invaders from 1978 did not calculate the average time between frames and updated as fast as the hardware could manage. When enemies were destroyed, there was less to render on the screen, therefore there were more frames per second. The difficult adjustment was not coded in but purely down to how fast each frame could render on the screen.

Any variable that is set as public will show up in the Inspector window. But what if a variable does not need to be public and you still need to be able to access it in the Inspector window? All you need to do is add the attribute SerializeField to the global variable:

To restrict the player movement and ensure you cannot move all over the screen, you can use if statements. Unity has several great math functions that make the code much cleaner. With function Mathf.Clamp you can clamp a variable between two values:

If you want the player to wrap around the screen, you will still need to use if statements but all you need to do is to set the x value to the opposite, e.g. 11.3 changes to -11.3:

Lastly, set the transform position to the new position:

I also added an extra function to give the player the ability to toggle the wrapping on and off:

I then check if this is true or false in the PlayerMovement function:

The complete script:

Good luck!

Unity / C# Game developer