Search Unity

Question How to do foot ground placement with existing animations?

Discussion in 'Animation Rigging' started by Ragueel, Apr 15, 2021.

  1. Ragueel

    Ragueel

    Joined:
    Jun 2, 2018
    Posts:
    39
    I am currently trying to ground the character's foot. It is a non-humanoid rig with multiple feet. It already has animations of idle, walk, run, etc., created in Maya. I haven't found any tutorials or explanations for achieving foot placement with the animation rigging package. Is it possible?

    I tried using Two Bone IK, but it fully overrides foot animation, which means foot movement is lost.

    The only tutorials about foot grounding are all using humanoid rigs without the animation rigging package. So how to do foot grounding?
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    I haven't tried this myself yet, but I have an idea how this might work.
    When you're importing your animation clips in Unity, you can add AnimationEvents to your frames in the import settings. These Events will look for a component on the model object with a corresponding method signature (name and parameters) and call that function whenever the animation enters those marked frames.
    I would create a script with a method IKLegUp and IKLegDown, with maybe an integer argument to identify which leg you're manipulating (since you have multiple feet).
    Then, in those methods you can toggle the IK controls on and off, and place the IK targets usig raycasts from the feet bone to determine where they have to go.
    Then you simply add the necessary event calls on the frames a foot has to make contact or stops making contact with the floor, and you should have a solid base to go from.
     
  3. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Just popping back in to say I've tried implementing this myself and managed to get a (rough) working prototype!
    Video showing it off here at my twitter.

    Like I said earlier, I imported a walk cycle and in the import settings I flagged certain keyframes with Animation Events labeled FootStepDown and FootStepUp, with the integer parameter for the left foot set at 0 and for the right foot at 1.

    Then I wrote a new component with the methods FootStepDown(int footIndex) and FootStepUp(int footIndex) and attached this to the root animation object in the scene.
    I gave it references to the left and right foot IK controls, and assigned these the same numbers so that whenever one of those methods gets called, it will check what foot to execute upon.

    Now here's the slightly trickier part. Each foot control got a new component as well, called IKControl.
    IKControl is responsible for figuring out where it needs to be whenever it's active.
    To do so, I gave it a Toggle function to turn it on and off, which gets called in FootStepDown and FootStepUp.
    When it gets turned on, it casts a ray down from the corresponding foot bone to find the point where it needs to be, and then also calculates the orientation. It will then also flag itself as active.
    If we turn it off, we simply turn the active flag off.
    In the IKControl's Update method, I turn the matching Rig component on or off (set weight to 1 or 0) and then also set the Control's position and rotation to the calculated position and orientation each Update.
    The reason for this is because your control has to be a child of your animated model, so if the model moves your IK Control will normally move along, which is usually not what you want.
     
    Ragueel likes this.
  4. Ragueel

    Ragueel

    Joined:
    Jun 2, 2018
    Posts:
    39
    I also implemented my own method, but without events. What I did was I raycasted to the floor and checked the distance to it. If it is in a certain threshold then I grounded foot otherwise I played the original animation.
     
  5. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    That could certainly work though I'm unsure if that remains stable when you're dealing with uneven terrain.
    As you can see in my video, on the slope up walk the feet already do intersect with the ground they're supposed to stand on (something I should refine) and in those cases continuous raycasts would still provide false results.
    Personally I cast with an offset right now (it would probably be better to do an area or volume check) to work around this.
    I'm also guessing it's a little more efficient to only cast on certain animation frames than continuously casting (of course, if you're doing this on limited actors, who cares?), but I'm also not sure how you turn off the IK control when the foot is supposed to lift? Do you just let the limbs overstretch until the raycast breaks?
     
  6. echolink

    echolink

    Joined:
    Apr 30, 2014
    Posts:
    5
    Hi Ragueel, can you explain a bit more how you switch between original animation and rig constrain while raycasting?