Okay, now we have a player in the scene, but we have to make him move, right?
What we want is this: the player has to move on the X axis to the left or right and the Y axis up and down.
If we grab the Player in the scene and drag it around, we see the coordinates in the inspector change and update the position.
When I drag up and down, the Y axis changes, when I drag left and right the X axis changes.
Now, Unity has some methods to move objects. One of them, for example, is the Translate method, but to learn more about this we need to understand how to use and read the SCRIPTING API.
Without any doubt, this is the CORE of programming logic.
Every time we have a problem, to resolve it we need to research, and the documentation is the best place to search a solution.
The easiest method to find the reference of what we neeed, in this case, the transform componet, is click the little question mark in the inspector
The page which opens is the reference manual to the transform component.
To search the Scripting API , go in the research space, click on Scripting API and digit Transform.
A little tips : when we are the scripting API, and find solution, we don’t lost in the example. What we need are the function, and the syntax.
We only need to know what the function does and how write it correctly. That’s it.
In the same page, we scroll down and we see properties, like rotation or scale and some more but for now they are not not important.
In the public methods section, at the bottom, there is a
the function we need : Translate.
As I said before, the only important part are : what the function does, and in this case, the description says : “Moves the transform in the direction and distance of translation.” and the syntax : Transform.Translate() and we have to pass a direction.
In the example we see the function writes in the Update method.
So, open Unity, and in the script we type
and we pass the direction, for example Vector3.right, that means move the object on the positive X axis.
Positive direction are : right on X, up on Y and forward on Z.
Negative direction are : left on X, down on Y and back on Z.
The result is : transform.Translate(Vector3.right);
And it end with semicolon.
Every line of code end with semicolon.
Now save, go back to unity and press Play
The Player go to right incredible fast, and if we look in the inspector we see it continuously go to the right without stopping.
First of all, maybe you notice I don’t use the “new” keyword in front at Vector3. There’s an explanation : normally we have to use the key new and, in the Vector3 case, pass the value of that Vector.
But Unity has some predefinite methods, like Vector3.right, which are the same thing of “new Vector3(1, 0, 0);"
Write this : Vector3.right
and this : new Vector3(1, 0, 0)
is the SAME THING
If you use the negative sign in front of 1, it moves on left
Unity has this type of method for every direction, left, right, up, down, back and forward, and every of this make object move of one unit in the desired direction.
Plus, there are two methods, Vector3.zero and Vector3.one.
The first set the position to 0 in every axis, the second set the value to “POSITIVE ONE” in every direction, so RIGHT ON X, UP ON Y AND FORWARD ON Z.
In the next article we see how to make the object move with correct speed.