0
\$\begingroup\$

Given two variables, a Vector3 representing body velocity and a Quaternion representing body's local rotation, how could I normalize the velocity vector so that it's always oriented towards the default Vector3.forward direction?

The context of this problem is that I have a 2D blendtree for character movement animations. I wanted to use the RigidBody.velocity to control the two blendtree axes. This did not work as I initially expected because, depending on body rotation, the velocity also rotates to that direction and this when I'm moving forwards but my body is rotated 90 degrees to the right, my right (x) velocity would be 10 and my forward (z) velocity would be 0. In that case I would like to, using the current body rotation and velocity values, "normalize" the given velocity vector so that it has 10 in forward (z) velocity and 0 in right (x) velocity any time the entity is moving forward, no matter it's orientation.

I've tried playing with Vector3.RotateTowards and Quaternion.LookRotation but I haven't gotten the desired results and I don't really know what to look for.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Did you want this?

var localVelocity = transform.InverseTransformDirection(worldVelocity);

or

var localVelocity = Quaternion.Inverse(orientation) * worldVelocity;

This gives you a vector in the local coordinate frame of the transform/quaternion, where the z component is the forward velocity, the x component is the sideways velocity, and the y component is the upward velocity.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .