Simple Player Movement in Unity
- Create a new scene called Game
- Create a cube, call it player and apply to it a new blue material. Move it at bottom
- Change aspect ratio to 16:9
- Ctrl + S or Cmd + S to save Scene
- Create a new script by making right click on project/Create/C# script. Call it Player. Open it.
Namespaces (using keyword): libraries with code we can use
For example, if we don’t write using UnityEngine, we must write UnityEngine.Monobehaviour
Monobehaviour: it allows us to drag and drop scripts or behaviours to the GameObject to apply logic (to control it). It inherits from Behaviour class, this inherits from Component and this inherits from Object. More info here
Start: called when the scene starts just once
Update: game loop, called once every frame
- Attach the Player script by using drag and drop on Player GameObject in hierarchy
Now we can access the Player GameObject components through the script. How can we move it?
We want to modify the Position values in Transform component in inspector
To do that, we need to access this component in the script
Here it says that position is a Vector 3. We can get its values or set its values
The same applies for Rotation and Scale
But, what is a Vector3?
Basically, a Vector3 is the way we tell the Engine, for example using position, where we want the GameObject to be placed at certain moment. For that, we need to set values for 3 axis (x,y,z) to build a Vector. That’s why it’s called Vector3.
What is the actual state of the player?
Its position is x=0, y=-3, z=0
What we want to do?
We want the player to be placed in position x=0, y=0, z=0 when the scene begins to play
How do we do that?
Use start method: the first frame when the scene is played
set the transform.position to the desired position
Keep in mind: Unity has the info about the actual position of the player, it is Vector3(0,-3,0) and those values are already known by the engine, but the engine doesn’t know about any other position, so we need to create a new Vector3 to indicate the engine where we want to place the player, it is, which will be the new transform.position values
Moving the player
When we select the player and moving it
- To the right in x axis, position x value is increased
- To the left in x axis, position x value is decreased
- To the top in y axis, position y value is increased
- To the bottom in y axis, position y value is decreased
To make it move in runtime (while in Play mode), we can search info about Transform in Unity manual clicking on the question icon next to Transform
This is the manual. Click on Scripting API and write Transform in the input inbox at right. From the results, select Transform
Here you’ll find info about properties, methods and others.
As we want to move the player, we’ll check out Translate
As it says, it needs a Vector3
In the script, we need to move the player from one point to another, but not instantaneously, but frame by frame.
In this case, we will use the Update method.
There are 2 ways to move the object to the right :
If we want move it to the left, simply change right by left in first line of code or change 1 by -1 in second one.
Now, if we execute the code as it is, the player will move extremely fast.
It is because the Update method is executed 60 frames per second, and the Translate method moves the player 1 unit each time it is called, so we are telling the engine “move the player 60 units per second”. This is called Frame rate dependency
To move the player 1 unit per second we use Time.deltaTime
And if we want move it 5 units per second, just multiply Vector3.right * 5
How does it work?
It applies the distributive property of multiplication