How to turn input axes to vectors, relative to the way an object is facing?

Hello!
I am currently working on a procedural walking animation script.
To calculate where the next step will be, I am currently using:

lastStep = newStep;
newStep = transform.position + (transform.InverseTransformDirection(movementNormal) * stepLength);
newStep.y = RaycastGround(newStep).point.y;

movementNormal is a Vector3, made from new(Input.GetAxisRaw("MovementX"), 0, Input.GetAxisRaw("MovementY"));

When the player faces the global z axis, the steps are placed correctly. However, when the player faces the global x axis, the steps seem to be offset to the right. This is the only pattern I noticed. The script acts wonky all the time.

Seemingly, the problem is that I can’t get the movementNormal to successfully translate into local a local direction.
Can anyone help me?
Thank you!

The idea is to multiply the input vector by the rotation in question, then use that rotated vector instead.

Sometimes you flatten the rotation (eg, use only its heading).

You can see this happening here, around lines 124 to 130:

Demo scene in the full project… proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

1 Like

Catlike Coding’s orbit camera tutorial has a small section on it: Orbit Camera

Scroll down to part 3 and you can see this illustrated.

Hi!
Thanks a lot for the quick reply.
I don’t really understand the script you quoted.
Basically what that line does is: Vector3 rawInput * Quaternion.Euler(0, transform.eulerAngles.y, 0) but like expected, that yields an operator error. You can’t multiply a Vector3 by a Quaternion.
Also, simply multiplying rawInput (in my case movementNormal) by transform.rotation.y also doesn’t get a better result.
Something I’m missing here?

Yes, yes you can, and you didn’t copy the code I provided correctly.

It must be rot = quat * rot

It must NOT be rot = rot * quat

See this line:

var RotatedMoveInputs = controlRotation * RawMovementInputs;

It works, really. Trust me, I’ve been rotating vectors with quaternions for a long time. :slight_smile:

1 Like

Haha!!! I would’ve never thought that the order of the multipliers mattered!!! The more you know :slight_smile:
Thanks a lot, it works just as intended.
Have a great day!

1 Like