# Mecanim IK Foot Rotation

I am working on some foot placement and so far have my characters feet aligning to the ground nicely, however the code snipet I rely on to do this:

Quaternion footRotation = Quaternion.LookRotation( Vector3.Exclude( hit.normal, transform.forward ), hit.normal );

forces the foot to always face the same direction as the transform. What I’d like to do here is to have the foot point in the direction it normally would before applying IK rotation weight.

Seems like that would require “transform.forward” in the above snipet to change, but i’m unsure of what to use. If i grab the bone transform for the foot and use its forward, things go all wrong :). Also tried some Vector3.Cross calls with the foot up and transform forward, etc… some combinations come close but still fail.

Just for a visual on what happens with this code so far, here are two SS’s:

1. Without IK (natural foot rotation at idle): http://screencast.com/t/N5ZVh5NSzzPu
2. With IK (facing transform forward): http://screencast.com/t/2obcpS7VD

I’d like to have the Vector3.Exclude working so that the foot is planted using IK as in (2) above, but uses the animations foot rotation (on the yaxis?) as in (1).

Can someone with some strong math skills help me out here :)?

Interesting question! What you apparently want to do is restrict the IK to the actor’s x axis. Here’s how I’d do it:

1. Get the cross product of hit.normal and transform.right which results in a vector that is basically the slope corrected transform.forward.
2. Get the quaternion for the rotation between your actor’s transform.forward and the vector calculated in 1.) by using Quaternion.FromToRotation.
3. Multiply your foot’s transform.rotation with the quaternion resulting from 2 (which basically ‘adds’ these rotations).

I have not tested this myself, but I think it should work that way.

Ok interesting, this does what appears to be the exact opposite of the desired effect, maybe negate something in it?

http://screencast.com/t/nLzek7KU4aS

I seem to have it fixed with using:

Vector3 slopeCorrected = Vector3.Cross( hit.normal, foot.right );
Quaternion footRotation = Quaternion.LookRotation( Vector3.Exclude( hit.normal, slopeCorrected ), hit.normal );

So that’ll do! Now its time to debug the “pop” i get with that foot twisting about lol, probably something to do with my code trying to predict for walking to enable/disable where clipping is not involved (i.e. avoid dragging feet).

Thanks!