- Home /
transform.TransformDirection(transform.forward) (0.0, 0.0, -1.0)
With the gameobject's forward vector pointing to global positive x...
transform.TransformDirection(Vector3.forward) outputs (1.0, 0.0, 0.0) ... OK, understood.
transform.TransformDirection(transform.forward) outputs (0.0, 0.0, -1.0) .... Why?
Can someone explain why #2?
Answer by Bunny83 · Nov 07, 2017 at 02:04 AM
This line of code makes no sense. "TransformDirection" takes a local space direction vector as input. transform.forward is a worldspace direction.
Basically Those two lines are identical:
Vector3 fwd = transform.forward;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Your code does interprets a worldspace direction as localspace direction and transforms it again. The result will be nonsense.
So this line:
transform.TransformDirection(transform.forward);
would be the same as doing
transform.TransformDirection(transform.TransformDirection(Vector3.forward));
Just to clarify / explain the result of (0, 0, -1):
Vector3.forward is just the constant (0,0,1). Your gameobject is rotated 90° to the right. So everything in localspace is rotated 90° clockwise. "transform.forward" is the worldspace forward direction of your object which is in your case (1,0,0). If you treat this as localspace direction (so within the localspace it points to the "right" of the object's own rotation) and transform it again into world space (effectively rotating it 90° clockwise) you end up at (0,0,-1)
Came back to this post and realized I have one more question, for the same example above:
Why does transform.InverseTransformDirection(Vector3.forward) return (-1.0, 0.0, 0.0) ?
And if the rotation is the opposite of the example (ie: -90) it returns (1, 0, 0)
I was expecting it to be the other way round: (1, 0, 0) when the object was looking to the positive X global axis
InverseTransformDirection transforms a worldspace direction into a localspace direction. If your object is rotated 90° to the right (cliockwise when viewed from above) the objects's x-axis (the red one) will point along the negative world z direction. So it points in the opposite direction of the world z axis (the blue one).
Of course when you have a worldspace direction of (0,0,1) it will come out as local space direction (-1, 0, 0).
If you rotate the object counter clockwise 90° the objects local x axis will be aligned with the world space z axis. That's why in this case the worldspace (0,0,1) direction will come out as (1,0,0)
Your answer
Follow this Question
Related Questions
Using TransformDirection incorrectly? 2 Answers
How should TransformDirection be used? 1 Answer
Gyroscope.attitude = GetComponent ().TransformDirection ? 1 Answer
Help understand moveDirection = transform.TransformDirection(moveDirection); 1 Answer
How to have correct strafe animation based on the players rotation? 2 Answers