Mecanim Foot Placement

Hello! I’ve been looking for a tool to make foot placement on Mecanim for a while now…
I couldn’t find anything, from simple to complex solutions, so I had to come up with my own thing (this is at the simple side of the rainbow):

This basic functionality is what you see on most games, just a simple raycast based approach instead of going crazy on detailed placement.
I mean, this is nowhere near Rune’s Locomotion system, but may be of some use for your retargeted Mecanim characters.
So I was wondering if anybody else would need this. Note that this tool is based on Mecanim IKs, so it is Unity Pro only.

Why use this?:
º Works with Mecanim.
º Works on any retargeted animation.

How it works:

Judging by the fact that every Mecanim character requires an Agent/Controller script I decided to make this a extension for the Animator class instead of attaching one more MonoBehaviour or forcing the need to change too much code on every controller script.
Once you have the class imported, every Animator instance you have gives you the option to use the FootPlacement() method inside of your controller script; Ideal results comes from using this inside of OnAnimatorIK().

For basic heel placement all you need is to tell the function who the pelvis is and decide on IK weights and foot offset for fine adjustment.
For detailed foot aligment, adding a empty GameObject on each foot is required because a reference for Raycasts is needed in world space instead of joint’s local space.
So, a controller using this function with normal aligment would be like this:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animator))]
public class MyController : MonoBehaviour {
    public bool Do;                                                       // If need to turn off placement for any reason.
    public float HeelOffset;                                           // Fine adjustment of heels.
    public float FeetOffset;                                           // Fine adjustment of feet.
    public Transform Hips;                                            // The Pelvis joint, just a height reference.
    public Transform LeftToe;                                       // Foot joint used as foot length reference.
    public Transform RightToe;                                     // Foot joint used as foot length reference.
    public Transform LeftTip;                                        // Empty GameObject, Joints' world space reference.
    public Transform RightTip;                                      // Empty GameObject, Joints' world space reference.
    private Animator Actor;                                           // The Animator component attached.
    //
    void Start() {
        Actor = GetComponent<Animator>();
    }
    //
    void OnAnimatorIK(int Layer) {
          Actor.FootPlacement(Do,HeelOffset,FeetOffset,(Actor.GetFloat("Speed")/10),Hips,LeftToe,RightToe,LeftTip,RightTip);
    }
}

… And the extension class will try its best to do the hard work. :slight_smile:
I did this thing in few hours and I think is not very hard to replicate it; Also, is Unity pro only so I don’t know if anybody would be interested on this class. If you are interested just let me know, I may upload it for free or a very low symbolic price, dunno.
The code is a open C# class as well, so you can modify/extend it for your particular needs.

[Help Files]
You can download the ‘Guide’ PDF from here: http://www.bedroomdeveloper.com/guides/mfp.pdf

1 Like

Looks pretty darn awesome. Great work

Fabulous buddy,fabulous :')

Looks great - can’t wait to play with it. Will it be tough to make it work with 4 feet? :slight_smile:

Now that is really cool. Very nice work.

Thanks guys.

I didn’t test with quadrupeds, but I think you can simply call animator.FootPlacement() twice, on the first one you set the front legs and on the second call you set the second pair of legs. :slight_smile:
The only small overhead of this is you have two raycasts for each foot, so calling the function twice isn’t much of a problem.
I will look for a quadruped Mecanim animated model and try it this weekend and see if anything must be changed for such generic rigs.

Edit: Oh and I don’t know if it has changed, but isn’t Mecanim IKs for humanoids only? I need go back and read docs again, last time I checked was 2012 and IKs was for humanoids only.

Nice job Bruno! I recently did this myself (feel free to test it here)… mine’s quite simple though, no fine adjustments.

One thing you should keep in mind is players will almost always be using some sort of collider… and that makes it very difficult to use foot placement as you currently have… because the lower of the two feet needs to be able to reach the lower surface, but the collider won’t let it. To solve this I lerp my collider center up, based on a value determined by the difference in height between the two foot raycast hits. Results in something like this:

Also, if your character is moving you’re probably going to always want to use curves to set the weights on the feet IK, otherwise when they’re walking around they’ll just be shuffling their feet everywhere! :slight_smile:

That appears to be true. However, the documentation states this:

So maybe this feature is coming at some point?

Yes, looks like they didn’t change much since last year.
One thing I don’t like about Mecanim is that the responsible team want to do it all themselves and keep the API too much closed. That way there’s nothing we can do but wait for them to add a feature someday when they want doing so.

You know, you can tell the raycast a expecific layer to hit. For example, in my projects I use layer 10 for floor and layer 15 for level objects, so I tell the raycast to hit layer 10 and 15 only and it will ignore character’s colliders.

Yes, I was thinking about curves at first… But then I remember that some animation sources can’t have Mecanim Curves on them, so at my case I decided to ignore this option. But is easy enough to pass to the function a curve value instead of movement speed like I did, don’t even need to change the functions.

Oh no, I’m not referring to the raycasts interfering with the character collider, I’m referring to the collider interfering with the ground, by keeping the character too high for the lower foot to ever reach its raycast hit point. Like on ramp in your example, see here:

1261172--55358--$footik-bad.jpg

You can see the ik rotation is fine, but the right foot will never reach its raycast hit position because the collider is too high on the ramp, and its not like it can “stretch” outside of the collider. But if you move the character’s collider center up based on the height different of the raycast hits:

1261172--55359--$footik-collider-up.jpg

Hmm, I see. I wasn’t thinking about this because all my tests was with NavMeshAgent controllers instead of CharacterControllers.
With NavMAgents that doesn’t happen.

Doing it that way to fix this issue, you could also subtract collider height to make its ceiling to keep matching character’s head.

Very nice. This something that I want to create by my self.

Yea this is excellent. Great job, both of you! :smile:

Thank you guys! This is in fact not very hard to do with Unity pro. lol
The biggest deal is when you need to turn it on/off when character is jumping etc, involves a few functions to guide that or a lot of Mecanim curves on every clip you use.

@PrimeDerektive
That problem you posted is really related to CharacterControllers on big inclinations only. Is due to its geometry, and adjusting its center may be really annoying.
I’m glad in my project I am doing this for NavMeshAgents only, that really doesn’t happen with them. I tried a CharacterController at same character and I had to read the distance of foot to IK and use it as a offset for the controller’s center:

But for the NavMeshAgent, inclination must be more than 80º to see the IK unable to reach its goal:


UT decided to publish my basic solution for foot placement on Asset Store:

Very interesting work! Do you have any videos of a character walking on an uneven terrain mesh?

On a mesh similar to this:
1273250--56752--$terrain.png

Hi Lax. I try to not build such irregular terrains so I didn’t try it with anything like that.
But I will check it out and I can upload a video, but not today. What would be the size of a character placed on top of this mesh?

Anyways, I’m leaving by now. I may check this out by monday or maybe this weekend.

Nice work Bruno, I will also explore that :slight_smile:
Cheers

Thanks! that would be great. I was thinking that a human would be simliar in scale to the capsule here:
1273436--56770--$preview.png