Simple Player Movement in Unity
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
Open the Player script in Visual Studio.
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.
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.
A Vector3 direction to clarify what Axes we are moving.
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.
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.