How to find local position relative to facing direction?

Hi folks,

There is probably a simple answer to this but I have been having trouble figuring it out and have not found any solutions on here or other forums.

Basically, I am trying to determine how far a child object has strafed to the left or right relative to its direction.

This is very easy if the object is facing along the z axis.

It is transform.localposition.x

When the object rotates -90 and is now facing to the left, the strafe is now on z axis.

So it would be transform.localposition.z

The question is how to get this strafe distance for any facing angle (not up or down just left/right) ie 37 to the right or -16 to the left etc.

Cheers,
GreenLotus

It’s not, actually.

If an object’s local y position is at 5, and you rotate the parent, the y position remains exactly the same. That’s why it’s a local position.

float StrafeDistance=Vector3.Dot(child.transform.localPosition,child.transform.right);

Thanks zulo3d!!
StrafeDistance=Vector3.Dot(child.transform.localPosition,child.transform.right); works perfectly. :smile:

Dot product is go!!!

P.S. Apologies spiney199 - I could perhaps have have been clearer in my initial post regarding the axis of rotation and which object is rotating.