Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. StarShade88

    StarShade88

    Joined:
    Jan 28, 2020
    Posts:
    4
    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.

    Code (CSharp):
    1. public class IKAnimationPhysics : MonoBehaviour
    2. {
    3.     [SerializeField] Transform raycastY = null;
    4.     [SerializeField] Transform[] legs = null;
    5.     [SerializeField] LayerMask IKLayer = new LayerMask();
    6.    
    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;
    14.            
    15.             if(hit.point.y > leg.position.y)
    16.                 leg.position = new Vector2(leg.position.x, hit.point.y);
    17.         }
    18.     }
    IKProblem.PNG
     
  2. Le_Magnus

    Le_Magnus

    Joined:
    Jun 26, 2016
    Posts:
    5
    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