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
  3. Dismiss Notice

OnAnimatorIK() only works on the same GameObject?

Discussion in 'Animation' started by LaneFox, Apr 19, 2015.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,605
    I'm trying to have a wrapper object control the Animator IK but it doesn't seem to work. The OnAnimatorIK() is not called unless its on the same object as the Animator component. I suppose it makes sense.

    I suppose the only way to handle this is to have a proxy which is placed with the Animator that handles the OnAnimatorIK() pass while i control variables from the parent object's scripts?

    Example code I'm using below. Is there a different way to do this or do I need to make a proxy?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class IKthing : MonoBehaviour
    6. {
    7.     public Transform objToPickUp;
    8.     public GameObject goWithAnimator;
    9.     private Animator animator;
    10.  
    11.     void Start()
    12.     {
    13.         animator = goWithAnimator.GetComponent<Animator>();
    14.     }
    15.     void OnAnimatorIK()
    16.     {
    17.         Debug.Log("Doing IK pass.");
    18.         float reach = 1f;
    19.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, reach);
    20.         animator.SetIKPosition(AvatarIKGoal.RightHand, objToPickUp.position);
    21.     }
    22. }
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Right, we didn't want to broadcast the callback to other game object because it could decrease performance.

    You can make a proxy on your animator's game object that forward all your OnAnimatorIK to another script.
     
    Skunk-Software likes this.
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,605
    Yep, I moved forward with that. Thanks for confirming. =)
     
    Batataglins likes this.
  4. Baelgren

    Baelgren

    Joined:
    Jun 26, 2014
    Posts:
    5
    Very sorry to revive this post but how does one "make a proxy on your animator's game object that forward all your OnAnimatorIK to another script"?
     
  5. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    You make an OnAnimatorIK method on your Animator's GameObject, that calls your method on some other GO (where it makes sense for you)
     
    Batataglins likes this.