Get Local Direction Gameobject Is Moving

Right, so I am trying to develop a system where a character’s legs is moving depending on the direction it is heading.

(Character is locked-on to a box)

I got a system in place that moves the character along a X or Y axis.
But I am having problems finding a way of detecting where the character is moving relative to itself.

Currently testing with this

float xVal = transform.InverseTransformDirection(ThisRigidBody.velocity).x;
print(xVal);

But it just gives of rapidly spiking numbers that I can’t use. Any suggestions or alternatives :slight_smile:

I know it’s uncool to bump, but any suggestions or any additional info required? :slight_smile:

I think your problem is that you’re trying to compare a character direction (quaternion/matrix) with a rigid body velocity (just a vector, not a 3d transform).

You could use rigidbody.rotation but that won’t necessarily give you the direction it’s heading in as it could be pointing one way and moving another way.

If you want to do it in 2d you could just do something like this:

  1. Get the character transform.forward to get the vector it’s pointing towards. Normalise it
  2. Get the rigidbody.vecocity to get the vector it’s moving in. Normalise it
  3. Do a Vector3.Cross of those 2 vectors - the resulting vector.y will give you the angle between them (between -1 and 1, multiply by 180 to get an angle in degrees)