Execute code after the animator update but before IK happens.

Hi,

Currently, I am using 2D animation with 2D IK. To make the foot IK, I have to adjust the position of the foot target after the frame of the animation. I have tried LateUpdate. Unfortunately, LateUpdate is called after the IK has been applied…
I have already tried many possibilities.

public class IKAnimationPhysics : MonoBehaviour
{
    [SerializeField] Transform raycastY = null;
    [SerializeField] Transform[] legs = null;
    [SerializeField] LayerMask IKLayer = new LayerMask();
   
    private void LateUpdate() {
        foreach (Transform leg in legs) {
            RaycastHit2D hit = Physics2D.Raycast(new Vector2(leg.transform.position.x, raycastY.position.y),
                Vector2.down,
                Mathf.Infinity,
                IKLayer);
            if (hit.transform == null) continue;
           
            if(hit.point.y > leg.position.y)
                leg.position = new Vector2(leg.position.x, hit.point.y);
        }
    }

Hi.
Here is the way I solved this problem :

  • Create a second IK solver, with the same effector (same foot transform in your case) and with its own target.

  • Put this 2nd IK just below the 1st in the hierarchy, in order to make it override the 1st one by the IK Manager.

  • Disable the 2nd IK.

  • Get a reference of this 2nd IK gameobject in a script.

  • Now, when you want to override the 1st IK (because foot on ground for example), just enable the 2nd IK and modify its target.

  • Don’t forget to disable it when no override needed.

In my project, I set the target as the IK object itself so I have the same reference for move or enable purposes.

I hope there will be some new features in the future, to make it more convenient, like the IK Pass system for 3D humanoid rigging…

Hope it helped