The goal is to make a simple player movement script for a 2.5D game to get the player moving around.
First things first, open up a fresh New Scene in Unity.
Next, create a Cube that will be used as the Player. There are two ways to do this.
Now rename the Cube to “Player”.
If the scene has not been saved, save the scene. Make sure to save it in the Scenes folder to stay organized. Remember to save early and save often!
Now that everything done so far has been saved, it is time to create the script to control the Player gameobject. Before creating the script, it is best to stay organized and make a Folder for scripts. Create a folder and name it “Scripts”.
Time to create the script for the Player and name it “Player”. When naming a script, the script’s filename and the class name inside the script must be the same or causes an error. With the Player script selected, the Inspector window will show a preview of the script, and the class name is the same as what the file was named.
Now drag the Player script onto the Player gameobject and then open the Player script to begin programming the code to move the Player.
With the Player script opened, notice that the name of the class, which is Player is the same as what the script was named when created. If it is not, make sure to change it so it is, or else there will be an error.
Notice that after public class Player, there is : MonoBehaviour.
MonoBehaviour is a base class that every Unity script is derived from. This is known as inheriting and what that means is the Player class will have access to the MonoBehaviour class’s functions like Void Start and Void Update, but most importantly, it allows the script to be attached to a gameobject like the Player.
To make sure that the Player can always be found easily at the start of Play Mode, add a line of code in void Start. A transform is what holds the information of a gameobject, such as position, rotation, and scale. The information is stored in a Vector3 which is a set of 3 floats representing (x, y, z). So by changing the transform’s (the Player’s) position to equal a new Vector3 with the values of (0, 0, 0) at the start of Play Mode, it will move the Player to (0, 0, 0), which is the center of the screen. After the code has been entered, make sure to save the script (Ctrl +S), and make sure to save it every time you make a change, or else Unity will not know it has changed.
Now back in Unity, move the Player away from the center and enter Play Mode to see the script work. Now that the Player will always be in view at the start of Play Mode, let’s code the user input.
In void Update, add in a local float variable that will be used to hold the input from the Horizontal axis defined in the Input Manager.
Now to apply the input to the Cube. What transform.Translate does basically adds the Vector3 that is passed in the parentheses to the transform.position of the Player moving it each time Update runs.
The code is adding or subtracting 1 unit (meter) from the x of the Player every frame, so about 60+ meters a second.
Time.deltaTime is the amount of time in seconds since the last frame. By multiplying the Vector3 by Time.deltaTime, it lowers the amount the Player’s position is changed by.
It is moving too slow now, but it can be easily adjusted by adding a variable.
At the top of the class, create a new public float variable named “speed” and have it equal to 5. Now in the Translate function, put the Vector3 multiplied by speed multiplied by Time.deltaTime.
Now the Player is moving at a fairly nice speed, about five units (meters) a second.
If the speed needs to be adjusted, it can be done so right from the Inspector since the variable is public.
To implement vertical movement, follow the same process as before but get the Vertical axis and put verticalInput in the y of the Vector3.
Now the Player can move both vertically and horizontally.
To clean up the code a bit, create a local Vector3 named “direction” and have it be the new Vector3 with the horizontalInput and verticalInput. Then replace the Vector3 in the Translate function with direction.
With the Player moving vertically and horizontally, the simple player movement script is complete.