Converting between local and global direction

I’m using a forward direction vector of a gameobject like this: myGameobject.forward;

This vector however is a local direction. How can I convert direction into a global direction? And when I have a global direction, how can I convert it back to the local direction of a particular game object?

First a gameobject doesn’t have a forward variable, only the Transform component of a GameObject. This forward vector is always in world space. The forward direction in local space is a constant value:

Vector3.forward == Vector3(0,0,1)  // forward in localspace

myGameobject.transform.forward     // forward in worldspace

To convert between local and world space you can use:

Use Transform.TransformDirection / Transform.TransformPoint and Transform.InverseTransformDirection / Transform.InverseTransformPoint!

In your case, you’d use myGameObject.transform.TransformDirection(new Vector3(0,0,1)) to convert a forward direction to a global direction. And InverseTransformDirection to convert back from global to local space.

Maybe you know?:

How to calculate InverseTransformDirection if you have not transform and have only Vector3(position) and Quartanion(rotation)?