Simple Player Movement in Unity

Germán Walden
2 min readMar 29, 2021

--

Objective: Implement basic player movement on both vertical and horizontal axes adjustable with a handy private but serialized speed variable visible in the inspector.

Requisites: A Player Gameobject, in this case we will use a Cube and a Player Script

Basic movement implementation

Open the Player script in Visual Studio.

Empty Player script

Now we are going to create a private speed variable above the Start method that will allow us to modify the value on the inspector thanks to the [SerializeField] atribute.

Good practices in programming say that we should always keep every variable private unless the value is going to be accessed and modified by an external script. Designers will love having this feature.

The SerializeField atribute allows us to modify the value on the inspector

We will also need two local variables in the Update method that will store the Input information that our keyboard returns when we press the WASD or arrow keys.

Input variables storing Axes input information

A Vector3 direction to clarify what Axes we are moving.

Vector3 direction containing Input info.

And finally the transform.Translate Unity function to apply the direction to our Player multiplied with the speed value and Time.deltaTime to adjust the velocity to something closer to the real time.

Unity function to move our Player

This is what your script should look like. Now we can jump into the Unity Editor.

Remember: The speed variable can be modified also during a play test.

--

--

Germán Walden

Passionate Unity developer always looking for new challenges!