How set Player position
We have to take this script and attach on the gameObject to give it the behaviour.
What we want is : when the game start, we give the Player a position.
It means that after click play, the Player snap in the position we have set.
To do that, in Start we have to write PSEUDO CODE
PSEUDO CODE is one of most useful thing we can do.
We simply take one problem, divide in little problem, write it and start to solve that problem piece by piece.
Remember, to be a Game Dev / Software Engineer is the art to know how solve problems.
And the best practice to solve problems is take that problem and divide it in little problems.
First of all, we add the script at the gameObject like other component.
We can drag the script by the hierarchy or open the inspector and drag and drop it.
Same result, different way.
Now, to set or change position, we have to access to the “transform component” of gameObject.
So we write transform followed by a period
As you can see, after use a period, it opens menu to go further in other options.
It may be confusing at first, but imagine this as a Chinese box: to get to the option you need, you have to choose step by step what component you want to manipulate.
Let me explain: now we want to manipulate the transformation component because it sets the position of our object.
But maybe we only need ONE vector out of a total of three, maybe the x-axis, or maybe just the z-axis. To access to that part, we have to understand and think : what component i have to manipulate to access x axis or z axis?
So, with transform we access to transform component, if I add .position I can manipulate all of three axis at the same time, but if I type .x after .position it result : transform.position.x
This if we need to manipulate position, but in the same level we can manipulate everything inside the transform component, for example Rotation
Probably you notice that every time I use the dot notation, little menu show up for give us “little tips” of waht we can do.
We can move down an up with arrow key to choose
Now, in order to change the position, we have take our position and reassigned to a new position.
Every time we change or set position in Unity, we have to use something called VECTOR3, and use the NEW keyword.
Now, try to go deeper : we create a script and attach to gameObject, right?
We also type transform.position to ACCESS the transform of THIS GAMEOBJECT.
It means that if we remove the Player script from Player and attached the same script to Camera, we access to the “TRANSFORM” OF THE CAMERA.
So, when we type transform.position, we READ the actual position of gameObject, and reassign to a new position.
This is the way to do it
where = means we assign the actual position to a new position
now we use the Vector3 class, because we need to assign three Vector for new position
Every time we have to assign new position with Vector3, always use the = notation.
After open the parentheses, we need to assign three Vector to our position.
This method asks us for some parameters: no parameters, two floats for the x axis and y axis or three floats for each axis.
For now we pass all of three float for every axes.
At the end of the line, the ; notation means “end of statement”.
Every time we finish to write some behaviour or some command, we need to use it, or Unity and Intellisense will give us an error.
Than, SAVE. ALWAYS SAVE after change the script, or Unity don’t read the changes.
Remember, just look the name of the script : if you see an asterisk, you have to save. CTRL+S is the shortcut.
CTRL+S and we are ok.
Now, for checking, I move the Player on different x and y position
Now I go to Play mode
And that’s all for change position of objects in Unity.