Let’s say I have a Vector3 (1,0,0) called A which is basically the World’s Vector3.right vector. Now imagine that I also have a GameObject that is in position (0,0,0) and its forward vector is facing the negative X axis, so the direction will be (-1,0,0).
What I want is to obtain the vector that results from taking the vector A and placing it into the coordinate system of this GameObject (considering its forward (-1,0,0) and up axis (0,1,0)). So the resulting vector in this case would be (0,0,1) which is now the world’s forward vector and the GameObject’s right vector.
I have tried looking into InverseTransformDirection but with no success… Thank you in advance.
InverseTransformDirection is not working as expected even when using the simplest of examples:
I created an empty game object, with its forward vector facing the negative X axis.
I attached a very simple script to debug the different vectors, here’s the code:
using UnityEngine;
public class TestScript : MonoBehaviour
{
void Start()
{
Debug.Log(transform.up); # output is (0,1,0)
Debug.Log(transform.forward); # output is (-1,0,0)
Debug.Log(transform.InverseTransformDirection(Vector3.right)); # output is (0,0,-1)
}
}
The result was the following:
As I described before, what I was expecting was for the InverseTransformDirection to return me (0,0,1). Instead it is returning (0,0,-1) and I don’t know why.
Why would you expect that to return (0,0,1)? If the object is facing the negative Z axis, and the input world direction is to the right (the positive Z axis), the direction in the object’s local space is going to be (0,0,-1).
There is something wrong with your expectations and/or the way you’ve stated the problem. Converting Vector3.right into the local space of a transform that is facing left results in a direction with a negative Z value.
I believe StarManta’s right, it looks like you input the GameObject’s direction, where if you wanted to match your example in your first post you should have input the World’s direction. Instead of Vector3.right you would put in Vector3.forward, and would get out the Right vector.
I should clarify, that since you’re wanting to input the GameObject’s direction and get the World direction, you should use transform.right instead of the InverseTransformDirection jumbo. It was not clear from the original post which one of the two you wanted.
My expectation is simply because of this: imagine im facing a TV. If i turn all my body to the left, then my right hand will be facinf the TV. As simple as that.
There’s the problem, you misstated local vs world. Your right hand is your local Vector3.right, not world Vector3.right. You’re doing this conversion in the opposite direction as you intended to. Converting local to world space is what you actually want to do, and that is transform.TransformPoint and transform.TransformDirection.
Right, there seems to be simply a confustion about coordinate spaces. Vectors on their own have no meaning on their own. It depends in which context you use them or what meaning you give them. The constant Vector3.right is just the 3 number combination (1,0,0). When this is interpreted / used as a worldspace vector, it means the worldspace right vector. However if interpreted as a local vector, it means right in the objects coordinate system.
The result you are expecting in your original post is actually that you have a local space vector like Vector3.right that essentially lives inside the object and you want to transform it into a worldspace orientation. This is essentially the opposite and you would use transform.TransformDirection(Vector3.right). Though this is exactly the same as transform.right. This property returns the worldpace orientation of the objects right vector. So those two pieces of code return the exact same thing. When the gameobject is facing the negative world x axis, it means its worldspace right vector will point along the world forward axis.
Converting between coordinate systems just requires some spatial thinking. If you have trouble with the concept of vectors, coordinate systems and transformations, I can recommend the linear algebra series of 3Blue1Brown. It’s even worth to watch some of the episodes several times until the concepts really sink in.
ps: note that in math we usually use a right handed coordinate system to describe 3d space. However Untiy uses a left handed system. This is something you need to keep in mind when it comes to anything related to orientation, for example the cross product.