Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TransformDirection/InverseTransformDirection aren't working when an object is under a parent?

Discussion in 'Physics' started by Balan_ree, Apr 14, 2021.

  1. Balan_ree

    Balan_ree

    Joined:
    Jan 13, 2021
    Posts:
    3
    I have a rigidbody under a parent that's rotating. I want to modify the rigidbody's local velocity (velocity relative to the parent) on one axis. In the code below, I'm trying to move the rigidbody to the right, without changing the forward and up velocities.

    Code (CSharp):
    1. Vector3 localVel = transform.InverseTransformDirection(rb.velocity);
    2. localVel.x = 0.5f;
    3. rb.velocity = transform.TransformDirection(localVel);    
    However, the velocity on the other axis are impacted when the parent begins rotating. transform.InverseTransformDirection(rb.velocity) should output to (0.5, 0, 0), yet the y and z components change when the parent is rotated. As such, the rigidbody is not always moving to the right.