Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotate Vector3 to match GameObject's local coordinate system.

Discussion in 'Scripting' started by Serge144, Oct 18, 2021.

  1. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    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.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    For world-to-gameobject space, use transform.InverseTransformDirection(worldDirection) and transform.InverseTransformPoint(worldPosition).
     
    Bunny83 likes this.
  3. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    InverseTransformDirection is not working as expected even when using the simplest of examples:
    1. I created an empty game object, with its forward vector facing the negative X axis.
    2. I attached a very simple script to debug the different vectors, here's the code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestScript : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         Debug.Log(transform.up); # output is (0,1,0)
    8.         Debug.Log(transform.forward); # output is (-1,0,0)
    9.         Debug.Log(transform.InverseTransformDirection(Vector3.right)); # output is (0,0,-1)
    10.     }
    11. }
    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.
     

    Attached Files:

  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    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.
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    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.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    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.
     
  7. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    *its facing the negative X axis not the Z axis.

    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.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    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.
     
    Bunny83 likes this.
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    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.
     
    Last edited: Oct 18, 2021
  10. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    Thanks guys it is working now, although still not entirely sure how it works haha but I will look these things up a bit more.