missunderstatnding of transform.InverseTransformDirection(Vector3.forward)

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

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?
9285013--1301158--ray.jpg

Toggle Center / Pivot and Global / Local in the scene window, either with the buttons or with Z and X

Thank you for reply but i need a physical meaning. Why it works like that, if i rotate object clockwise the ray rotate conterclockwise?

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.

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
9285079--1301182--ray2.jpg

This is not correct. transform.forward is dependent on the orientation of the transform, Vector3.forward doesn’t ever change.

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.

I'm still not sure what you're expecting to happen.

Does this look correct or wrong to you?

https://www.youtube.com/watch?v=bRzGhYMNXXI

1 Like

transform.forward is the same as

transform.rotation * Vector3.forward

1 Like

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.

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.

2 Likes

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.

2 Likes

Thank you! Great explanation by link

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?

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).

1 Like

Thank you for suggestions