Question missunderstatnding of transform.InverseTransformDirection(Vector3.forward)

Discussion in 'Scripting' started by krou, Sep 10, 2023.

1. krou

Joined:
May 29, 2014
Posts:
22
Hello there! Can somebody help me to understand how it works? If i rotate object 90 degrees around it's Y-axis and draw a ray
Code (CSharp):
1. Gizmos.DrawRay(transform.position, transform.InverseTransformDirection(Vector3.forward) * 10f);
Why the Ray point in the direction of Vector3.left in world space instead of Vector3.right?

2. Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
37,184
Toggle Center / Pivot and Global / Local in the scene window, either with the buttons or with Z and X

3. krou

Joined:
May 29, 2014
Posts:
22
Thank you for reply but i need a physical meaning. Why it works like that, if i rotate object clockwise the ray rotate conterclockwise?

4. Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
37,184
I think it is because you are inversing a world vector (Vector3.forward).

What happens if you feed in Transform.forward? It should always return (vanishingly near) Vector3.forward.

5. krou

Joined:
May 29, 2014
Posts:
22
Code (CSharp):
1. Gizmos.DrawRay(transform.position, transform.forward * 10f);
In this case ray point in the same direction as local forward axis(z axis).
I thought that transform.forward and transform.InverseTransformDirection(Vector3.forward) should return the same result

6. AcidArrow

Joined:
May 20, 2010
Posts:
11,093
This is not correct. transform.forward is dependent on the orientation of the transform, Vector3.forward doesn't ever change.

7. krou

Joined:
May 29, 2014
Posts:
22
yes, Vector3.forward doesn't change but method transform.InverseTransformDirection(Vector3.forward) return result which depends on object orientation. And i'm trying to understand why that result as it is.

8. AcidArrow

Joined:
May 20, 2010
Posts:
11,093
I'm still not sure what you're expecting to happen.

Does this look correct or wrong to you?

krou likes this.
9. Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
37,184
``transform.forward``
is the same as

``transform.rotation * Vector3.forward``

krou likes this.
10. krou

Joined:
May 29, 2014
Posts:
22
For me it's look wrong. I don't understand why the ray is
also rotates. Perhaps I'm misunderstanding how an object in local space should work.

Last edited: Sep 10, 2023
11. halley

Joined:
Aug 26, 2013
Posts:
1,965
VectorInWorldSpace = TransformDirection( VectorInLocalSpace )
*
``transform.forward == transform.TransformDirection(Vector3.forward)``

*
``transform.forward == transform.rotation * Vector3.forward``

VectorInLocalSpace = InverseTransformDirection( VectorInWorldSpace )
*
``Vector3.forward == transform.InverseTransformDirection(transform.forward)``

*
``Vector3.forward == AMysticalFunctionTakingTheInverseOf(transform.rotation * Vector3.forward)``

It would usually be confusing and meaningless to do InverseTransformDirection of a world vector.

Post #18 in this thread is my earlier writeup.

krou and Bunny83 like this.
12. AcidArrow

Joined:
May 20, 2010
Posts:
11,093
You are converting a fixed world direction that doesn't move to a local one that does move.

You are converting world direction to local direction, and then you draw using the converted local direction as a world direction.

Assume we are talking about euler y angles. Vector3.forward is 0. Let's assume the cube is at +20 degrees in y. 0 converted to local space is 0 - 20 = -20, then you draw a line towards -20.

krou and Bunny83 like this.
13. krou

Joined:
May 29, 2014
Posts:
22
Thank you! Great explanation by link

14. krou

Joined:
May 29, 2014
Posts:
22
Thank you. If i understand right. When i rotate cube in Y at +20 degrees, the Vector3.forward becomes -20 and the angle between local cube facing and local vector3.forward will be 40 degrees?

Last edited: Sep 11, 2023
15. Bunny83

Joined:
Oct 18, 2010
Posts:
3,603
No.
``transform.forward``
returns the same as
``transform.TransformDirection(Vector3.forward)``
which both give you a world space vector. I don't know where you got the idea of using the inverse method from. You simply used the wrong method. So whenever you're dealing with vectors / points you should be aware of the coordinate system they belong to.

I think you're loosing yourself in certain details without understanding the bigger picture. This all just boils down to understand what a coordinate system / coordinate space is, how vectors and transformations work. On the bottom line this is just linear algebra. If you haven't watched it yet (or you watched it years ago) I would highly recommend to watch the essence of linear algebra series by 3b1b (again).

krou likes this.
16. krou

Joined:
May 29, 2014
Posts:
22
Thank you for suggestions

Last edited: Sep 12, 2023