Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question Execute code after the animator update but before IK happens.

Discussion in 'Animation' started by StarShade88, Nov 17, 2020.

  1. StarShade88


    Jan 28, 2020

    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.

    Code (CSharp):
    1. public class IKAnimationPhysics : MonoBehaviour
    2. {
    3.     [SerializeField] Transform raycastY = null;
    4.     [SerializeField] Transform[] legs = null;
    5.     [SerializeField] LayerMask IKLayer = new LayerMask();
    7.     private void LateUpdate() {
    8.         foreach (Transform leg in legs) {
    9.             RaycastHit2D hit = Physics2D.Raycast(new Vector2(leg.transform.position.x, raycastY.position.y),
    10.                 Vector2.down,
    11.                 Mathf.Infinity,
    12.                 IKLayer);
    13.             if (hit.transform == null) continue;
    15.             if(hit.point.y > leg.position.y)
    16.                 leg.position = new Vector2(leg.position.x, hit.point.y);
    17.         }
    18.     }
  2. Le_Magnus


    Jun 26, 2016
    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