- Home /
I need help understanding TransformDirection
Hi I've looked this question up before but usually the asker gets an answer that works around transform direction. I need to know what TransformDirection does exactly. No need to code just an explanation and an example will do. If possible you can also explain what this code does too.
function WalkForward()
{
var direction : Vector3 = Vector3 (1, 0, 0);
return transform.TransformDirection (direction);
}
Answer by EHogger · Aug 02, 2013 at 10:28 PM
You need to understand the difference between local space and world space. Local is for directions and rotations relative to the object, and world is relative to the game world. This function takes a local direction from an object and finds that direction in world space. Vector3(1,0,0) is the same as Vector3.right, which in the object's local space is the direction pointing to the right of the object. Depending on how the object is rotated, the output in world space will change.
This function is actually exactly the same as using transform.right btw
Hope that helps.
That function lets me turn my character and still always move to its right. But what I'm getting from your answer and the scripting reference is that no matter how much I rotate I will always move in the worlds X axis and that's not whats happening. I've tried InverseTransformDirection but that's not the result I come up with either.
It will always move toward the transform's right axis. [Editted for correction. Thanks]
Let's say the object is at 0,0,0 position and rotation. The local right would be the same as the world's right axis (1,0,0). If you then rotate that object so that it's right side is pointing up in the world, then this function would return (0,1,0) or Vector3.up
No, it will always move in the transform's X axis, which rotates with the transform. In the top left of unity is a button that either says Global or Local; when it's in Local view and you're in $$anonymous$$ove mode, you can see the local axes of the selected objects.
transform.TransformDirection(x) is exactly the same as saying "rotate the vector {x} by the rotation of transform {transform}"
transform.rotation * x == transform.TransformDirection(x)
Gotcha that's exactly what I needed. The scripting reference says transforms direction from local space to world space so I assumed it meant any movement would happen in the world axis so say (1,0,0) would move on the worlds X axis irrespective of my characters rotation. So if I use InverseTransformDirection will it then move in the worlds X axis even if my character is rotated?
InverseTransformDirectin does do the opposite, but your actual movement code will already be using world space, so you probably won't need that in this context.
Answer by Jamora · Aug 02, 2013 at 11:09 PM
To understand TransformDirection, you must first understand the difference in World and Local space. This answer does a pretty good job at explaining.
Now, TransformDirection, as per the docs, transform a Vector from local space to world space. In the world space, vector (1,0,0) is one unit to the right of the origo, in local space, however that same vector is one step to the right based on the object's current rotation. What Translatedirection does is it takes that relative movement, and returns how it is in relation to the origo.
You can see this effect for yourself if you imagine your monitor being the center of the world (0,0,0). The direction the back of your screen shows, is the positive z-axis(world forward), the direction the right edge of the screen is the positive x-axis (world right), and up is the positive y-axis (world up). Now, as long as you face your monitor, i.e. your(=local) forward is the same as the world (=your monitor) forward and your(=local) right is the same as the world right.
Now, if you turn so that your left is towards the monitor, the directions don't match anymore; your(=local) forward is suddenly pointing to the same direction as world right! But say you are blind and want to know which way you're actually facing in relation to the monitor. You'd need to do you.TransformDirection(you.forward)
and that would return (1,0,0), which is equal to the world right.
The code you posted returns the x-axis (i.e. way right) for the current object in world space. The same can be achieved by simply using transform.right
, because transform.direction is just shorthand for this translation.
Excellent answer. I wish I could mark 2 answers as correct. For anybody reading this, using the above blind example, if you turned left so that your right side was facing the monitor it would return (-1,0,0). If ins$$anonymous$$d you made a 180 and your back was to the monitor it would return (0,0,-1), etc. So if you used transform direction to make your character walk forward(0,0,1), then he will always walk to his forward no matter the rotation. If your character was laying on his back, transformDirection forward would make him start moving up in the world.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111439im_/https://answers.unity.com/themes/thub/images/avi.jpg)