0
\$\begingroup\$

I'm working in Unity and trying to visualize the forward direction of a GameObject. I came across two approaches:

Using transform.forward:

Debug.DrawRay(transform.position, transform.forward, Color.blue);
  

Converting world space forward to local space:

Debug.DrawRay(transform.position, transform.InverseTransformPoint(new Vector3(0, 0, 1)), Color.red);

Video: https://singh101404-gmail.tinytake.com/msc/OTM4NDQxOF8yMjk1ODg4Ng

I would expect both approaches to draw a ray in the same direction, representing the object's local forward direction. However, I'm observing some differences:

  1. When using transform.InverseTransformPoint(), the ray does not rotate with the object and seems stuck in one direction.
  2. The ray behaves as expected when using transform.forward, rotating with the object and representing its local forward direction.

Since I am creating a Vector representing the forward direction and then converting to local space relative to the cube shouldn't it produce the same result as trasnform.forward?

\$\endgroup\$
1
  • \$\begingroup\$ You may have confused InverseTransformPoint and InverseTransformDirection, and the inverse transformation with the forward transformation. \$\endgroup\$
    – DMGregory
    Mar 12 at 16:10

1 Answer 1

1
\$\begingroup\$

The problem here is that you're transforming a forward direction as though it were a point in space one unit "forward" from the origin.

Transform.TransformPoint() and Transform.InverseTransformPoint() are for transforming points, so they use the object's translation in addition to its rotation/scale. If your object is any distance away from the origin, that distance will tend to dominate over the piddly 1.0 in the input vector, and you end up with a vector that mostly points from the world origin toward the object, or from the object toward the world origin (for the inverse).

To transform a direction, you want to ignore the object's translation and scale and only use its rotation. Transform.TransformDirection() and Transform.InverseTransformDirection() do that.

Here you want the non-inverse case, because you're transforming a vector from the object's local space into world space, not the reverse.

So the corrected code would be:

Debug.DrawRay(
   transform.position,
   transform.TransformDirection(Vector3.forward), 
   Color.red);

or the equivalent:

Debug.DrawRay(
   transform.position,
   transform.rotation * Vector3.forward, 
   Color.red);
\$\endgroup\$
2
  • \$\begingroup\$ I printed the value of both transform.forward and transform.TransformDirection(new Vector3(0, 0, 1)) Both prints same value. If we are converting local space to world space shouldn't the transform.TransformDirection gives a different value \$\endgroup\$ Mar 13 at 16:08
  • \$\begingroup\$ No. TransformDirection goes from local to world as you want. It's InverseTransformDirection that goes the opposite way, from world to local. \$\endgroup\$
    – DMGregory
    Mar 13 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .