- Home /
transform.Translate() going nuts with Z/X axes?
From this tutorial: http://www.raywenderlich.com/4551/how-to-make-a-2-5d-game-with-unity-tutorial-part-1
I stopped right when he said kinda explained about translating the object's position based on the rotated plane, I honestly didn't understand the relation between the plane and the object, and why changing the Z axis in the script caused the object to change X axis instead (therefore moving "forward" in the orthographic view).
Can someone explain this to me? It would be much appreciated...
Answer by robertbu · May 04, 2013 at 07:14 AM
In working with Unity, you need to understand your frame of reference and the difference between local and world coordinates. Local coordinates are from the frame of reference of the object. Take any game object you create/import into unity with a rotation of (0,0,0). The side facing the positive Z axis will always be the local 'Z' for that object (also called the object's forward) no matter which way the object is rotated. When you bring the plane model into Unity (with rotation (0,0,0)), the front of the plane is facing 'Z'.
As a test, rotate the plane to a variety of angles and hit play The plane will always move towards it own 'Z' axis (i.e. forward).
Note you can change the behavior of Transform.Translate() by add the optional space parameter.
transform.translate(speed * Time.deltaTime,0,0, Space.World);
With this new parameter added, movement will be with respect to the world axes, not the local axes of the object. Change the code to use the above line. Rotate the plane to other arbitrary angle and hit play. It will go from left to right no matter what the rotation of the plane.
While I see how the suggested function call would work, I'm still intrigued with translate. The whole thing revolves around the Y rotation (in the tutorial it's 90), if I set to 0 the plane will face towards the background and it I hit play it will "enter" the background.
So does Y rotation define the plane's "forward" (I lack a proper definition sorry)?
And even if this is true, why changing Z affects X? Is there any scarry transformation internally that makes Z behave as X? (because if I leave normally, Y rot.= 90, X is increased ins$$anonymous$$d of Z, even though the script increases Z)
In the editor, 'Z' points towards the background, 'X' points to the right, and 'Y' upwards.
@Edit Now I'm totally confused, I set Y rotation to 45 (half 90 that is), the plane goes diagonally following where it is facing and both X and Z are incremented, the logic is clear to me but still, is Y rotation responsible for all these things?
Think about the plane the way you would think about yourself walking around in the real world. Your right side is always your right side no matter which way you face. Your front side stays the front no matter which way you face. In local coordinates, your front would be 'Z', your right 'X', and the top of your head would be 'Y'. It would not matter which way you faced. If I translate my self on the 'Z', I go forward no matter which way I face. Local means from the perspective of the object. And when I use the default version of translate, I move the object with respect to the local/personal axes of the object without any regard to the world axes.
So "my" Z will always go forward? If my object face right (after rotating 90º), I'll increase my Z axis no matter what that axis means for the world to move forward? Unity Editor shows me "X" increasing in the object's inspector window, I take that is the world's corresponding axis to my relative Z axis, right?
I'm slowly getting it, sorry for asking such a very trivial question, I tend to question everything. :/
@Edit Wow really, this just lighted up in my $$anonymous$$d, I guess that gizmo and those arrows to move the object are the actual world axes.
Your answer
Follow this Question
Related Questions
Moving objects together without parenting 5 Answers
Smooth transform position and rotation 1 Answer
is there any possible to use two transform.rotate function for same gameobject?? 2 Answers
Why does this script work correctly? 1 Answer
Aligning a transform according to two different positions and directions 1 Answer