- Home /
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?
Answer by Bunny83 · Mar 06, 2013 at 12:20 PM
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:
TransformDirection to transform a direction from local to world space
InverseTransformDirection to transform a direction from world to local space
TransformPoint to transform a position from local to world space
InverseTransformPoint to transform a position from world to local space.
Thanks mate this was awesome information but I think you have it backwards.
Vector3.forward == Vector3(0,0,1) // forward in Worldspace
myGameobject.transform.forward // forward in Localspace
No, it's not backwards, you may read it the wrong way ^^. Local space is a coordinate space just like world space is a coordinate space. The vector (0,0,1) is the objects forward vector in its own local space. Of course a vector is just a vector and can be interpreted in any space. However in worldspace the vector (0,0,1) would designate the worlds z axis and would not be related to any object.
In contrast transform.forward
returns the objects forward axis in worldspace. That's exactly what my comments said ^^. The first paragraph in the answer actually explain that already, no? This was the whole confusion that the OP had in the first place.
edit
ps: If you have trouble understanding coordinate spaces, vectors and linear algebra, I would highly recommend to watch the 3b1b series on linear algebra.
Answer by Philipp · Mar 06, 2013 at 12:15 PM
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.
Answer by ViktorCor · Nov 19, 2016 at 03:06 PM
Maybe you know?:
How to calculate InverseTransformDirection if you have not transform and have only Vector3(position) and Quartanion(rotation)?
Your answer
Follow this Question
Related Questions
transform shooting forward 2 Answers
Difference between transform.forward and transform.position.z? 1 Answer
How to anticipate the Vector3.forward of the enemy? 1 Answer
movement doesnt stop when jump pad is tapped at the same time 0 Answers
Setting an objects angle about a local axis without localEulerAngles? 0 Answers