Moving the Player at a Normal Speed
To understand how move object, we need to understand how work the UPDATE method.
The UPDATE is called ONE FOR FRAME, typically about 60 frame for second.
In the previous one article. we have see that Vector3.right move the object onu unit on right.
In Unity, one unit is equal to ONE METER in the real world.
Currently, we moving our “Player” ONE METER FOR FRAME, SIXTY METER FOR SECOND.
But what we want is move it ONE METER FOR SECOND.
So we have to convert one meter for frame to one meter for second.
To do this, we have to incorporate real time in the equation.
And is really simple, just add Time.deltaTime
transform.Translate(new Vector3(1, 0, 0) * Time.deltaTime);
or
transform.Translate(Vector3.right * Time.deltaTime);
Press Play, and see the result.
Little recap: if we put a Vector3 in the update method, the object move one meter for frame, if we multiply that Vector with Time.deltaTime, it moves one meter for second, ok?
But, if we want make the object move five meter for second? Or seven?
How we can do?
Well, simply multiply Vectro3.right for five and for Time.deltaTime.
Save and press Play
To explain better, what happen is this
Vector3.right multiply by 5 and multiply by Time.deltaTime, which is real time.
Vector3.right is equal to Vector(1, 0, 0), so we have
Vector(1, 0, 0) multiply by 5 and multiply by real time
Vector(1, 0, 0) * 5 * real time
So, the numbers inside parenthesys is multiply by 5
Vector(1 * 5, 0 * 5, 0 * 5) and multiply by real time
The result is : Vector3(5, 0, 0) * real time
Obviously, we can do this in every direction, positive or negative.
For example, if we have Vector3.left, and multiply by 5, this is what happen :
Vector(-1 * 5, 0 * 5, 0 * 5) and multiply by real time
The result is : Vector3(-5, 0, 0) * real time
So the object moves five meters for second TO THE LEFT.
Save and Play
In the next article we will see how create our first VARIABLE!