- Home /
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:
Without IK (natural foot rotation at idle): http://screencast.com/t/N5ZVh5NSzzPu
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 :)?
Answer by christoph_r · Jun 13, 2014 at 10:50 PM
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:
Get the cross product of hit.normal and transform.right which results in a vector that is basically the slope corrected transform.forward.
Get the quaternion for the rotation between your actor's transform.forward and the vector calculated in 1.) by using Quaternion.FromToRotation.
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.
Gave that a shot, but it turns out painful looking :) :
SS: http://screencast.com/t/Z8UYvUa2G8
Code:
Vector3 slopeCorrected = Vector3.Cross( hit.normal, transform.right );
Quaternion slopeForward = Quaternion.FromToRotation( transform.forward, slopeCorrected );
Quaternion footRotation = foot.rotation * slopeForward;
I was able to get something really close, using this:
Quaternion footRotation = Quaternion.LookRotation( Vector3.Exclude( hit.normal, foot.up ), hit.normal );
But it is just a bit off if you rotate around, the feet don't quite stay put.
That result is extremely weird because on a flat surface, slopeCorrected should be 0. Well... how about:
Vector3 slopeCorrected = Vector3.Cross( hit.normal, foot.right );
Quaternion footRotation = Quaternion.LookRotation(slopeCorrected, hit.normal);
Answer by Lypheus · Jun 20, 2014 at 03:50 AM
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!
Good to hear! And looks like it should be Vector3.Cross (foot.right, hit.normal) then (keeping in $$anonymous$$d that the cross product is anti commutative, i.e. its sign switches if you change the order of factors. Using that and dropping the Exclude part should improve performance a little bit (and make the code more compact).
Your answer
Follow this Question
Related Questions
Problem with rotations and IK 0 Answers
How to Aim Animator.SetIKRotation() to direction WRT hand bone rotation offset 0 Answers
How do you manually override mecanim's interpolation with rotations? 2 Answers
Mecanim ik curve 0 Answers
How to grab, pick and place an object to other location using Mecanim IK Script 2 Answers