Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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