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

Set Up IK In Editor

Discussion in 'Animation' started by ippdev, Jun 9, 2015.

  1. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    How do you set it up..what scripting is needed to get IK control in the Editor. This would be highly useful for setting script values. Right now I have to hit Play and record the positions by hand. In Edit Mode I could set the IK and Hints and click a button to record them to the appropriate script vars.

    Edit..To put it in perspective you have to hit Play...move the IK Goal and rotate it till yer happy..record it to the script as the Inspector window will wipe any changes when heading back to edt mode..then in edit mode you have to either rerecord those into arrays..which I am using..or hit Reset on the script component and relink everything tossed out by the Reset... which while work, is a hellofa lot less than recording 25 Vector3's by hand in the Inspector to match the script values you recorded. Then i you want Hints you have to move those in edit mode, hit Play and hope..more of a PIA than IK Goals as once you have hit the sweet pole vector spot you may still have to readjust the IK goals and record those.
     
    Last edited: Jun 9, 2015
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    I don't know, does OnAnimatorIk even make a pass in the editmode?

    Also, doesn't Skele (AS) do this?
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    I am not a big editor know-it-all Fox:) I spent four or five hours this morning trying/googling and reading to find this out to no avail. I tried a few things like ExecuteInEditorMode and nada.. And not being able to move elbow and knee hints in the Scene View when in PlayMode is a big hindrance to finding just where the sweet spot of an arm or leg position is. If you just can't currently then Mecanim devs need a kick in the pants to give it to us since we cannot see what is in their black box.

    What is Skele(AS)?
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I hear you, and now you will get what you deserve ;)

    Component are only ticked in play mode.
    To do what you want, you will need to tick by your self your animator component with animator.Update(); in a Monobehaviour with attribute ExecuteInEditorMode. And you also need to create a controller and set the base layer IK pass On.

    You can import this package in 5.1 and open up the scene test. You will see the complete solution.

    here the script as a reference
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [ExecuteInEditMode]
    6. public class EditorIK : MonoBehaviour {
    7.  
    8.     Animator animator;
    9.  
    10.     public bool enabledIK = false;
    11.  
    12.     public GameObject leftFootIKGoal = null;
    13.     public GameObject rightFootIKGoal = null;
    14.     public GameObject leftHandIKGoal = null;
    15.     public GameObject rightHandIKGoal = null;
    16.  
    17.     public float leftFootPositionWeight = 0;
    18.     public float rightFootPositionWeight = 0;
    19.     public float leftHandPositionWeight = 0;
    20.     public float rightHandPositionWeight = 0;
    21.  
    22.     public float leftFootRotationWeight = 0;
    23.     public float rightFootRotationWeight = 0;
    24.     public float leftHandRotationWeight = 0;
    25.     public float rightHandRotationWeight = 0;
    26.  
    27.  
    28.     protected GameObject CreateIKGoal(string name)
    29.     {
    30.         GameObject obj =  GameObject.CreatePrimitive(PrimitiveType.Sphere);
    31.         obj.name = name;
    32.         obj.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    33.         return obj;
    34.     }
    35.  
    36.     // Use this for initialization
    37.     void Start () {
    38.         animator = GetComponent<Animator>();
    39.  
    40.         if (leftFootIKGoal == null)  leftFootIKGoal = CreateIKGoal("LeftFootGoal");
    41.         if (rightFootIKGoal == null) rightFootIKGoal = CreateIKGoal("RightFootGoal");
    42.         if (leftHandIKGoal == null) leftHandIKGoal = CreateIKGoal("LeftHandGoal");
    43.         if (rightHandIKGoal == null) rightHandIKGoal = CreateIKGoal("RightHandGoal");
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void Update () {
    48.         Debug.Log("EditorIK.Update()");
    49.         animator.Update(0);
    50.     }
    51.  
    52.     void OnAnimatorIK(int layerIndex)
    53.     {
    54.         Debug.Log("EditorIK.OnAnimatorIK()");
    55.         animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, leftFootPositionWeight);
    56.         animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, rightFootPositionWeight);
    57.         animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandPositionWeight);
    58.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandPositionWeight);
    59.  
    60.         animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, leftFootRotationWeight);
    61.         animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, rightFootRotationWeight);
    62.         animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandRotationWeight);
    63.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandRotationWeight);
    64.  
    65.         if (enabledIK)
    66.         {
    67.             animator.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.position);
    68.             animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.rotation);
    69.  
    70.             animator.SetIKPosition(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.position);
    71.             animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.rotation);
    72.  
    73.             animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.position);
    74.             animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.rotation);
    75.  
    76.             animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandIKGoal.transform.position);
    77.             animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandIKGoal.transform.rotation);
    78.         }
    79.         else
    80.         {
    81.             leftFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
    82.             leftFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftFoot);
    83.  
    84.             rightFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightFoot);
    85.             rightFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightFoot);
    86.  
    87.             leftHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftHand);
    88.             leftHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftHand);
    89.  
    90.             rightHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightHand);
    91.             rightHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightHand);
    92.         }
    93.     }
    94. }
    95.  
     

    Attached Files:

    fherbst, raxter, EZaca and 5 others like this.
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    Thanks Pal. You rock:) I am sure many will have a use for this
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Very cool! Thanks for the tip =)
     
  8. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    I will be up at 6am messing with this.. As a clarification when you say "tick" my assumption is using the checkmark that toggles components off and on? And that I tick this on whilst in Edit Mode and Odd when i am in Play Mode? Or something else because I perceive a few ways to discern what methodology you are referring to in "ticking"
     
  9. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    By Component are only ticked in play mode I mean: Unity will call Component::Update only when you are in play mode.

    That why you need to add a Monobehaviour with ExecuteInEditmode and manually call Animator.Update to see the result in the editor.
     
    LaneFox likes this.
  10. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    @Mecanim.Dev I have the IK working In Editor now using the IK goals created. I simply dropped it on my Avatar root where the Animator is. (should I be doing something different?) I substituted my avatar IK goals and they did not work. Do I just get rid of their creation in the Start() function? Also.. Great you replied just minutes ago because just minutes ago i added the elbow and knee hints to the script and indeed they do react to them..meaning my internal avatar pole vectors per limb. However now I cannot adjust their position in Editor..This one last deal or understanding of it will make this super sweet for my and I am sure many others purposes. Here is my script..with sliders for adjusting weights.

    edit : Just found out that even when I shut the script off and adjust the elbow hints that they snap back to the previous position whether the enableIK bool is set to true or not. What is the trick here?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class EditorIK : MonoBehaviour {
    6.  
    7.     Animator animator;
    8.  
    9.     public bool enabledIK = false;
    10.  
    11.     //IK Goals
    12.     public GameObject leftFootIKGoal = null;
    13.     public GameObject rightFootIKGoal = null;
    14.     public GameObject leftHandIKGoal = null;
    15.     public GameObject rightHandIKGoal = null;
    16.  
    17.     //Pole Vectors
    18.     public GameObject leftKneeHint;
    19.     public GameObject rightKneeHint;
    20.     public GameObject leftElbowHint;
    21.     public GameObject rightElbowHint;
    22.  
    23.     [Range(0.0f, 1.0f)]
    24.     public float leftFootPositionWeight = 0;
    25.     [Range(0.0f, 1.0f)]
    26.     public float rightFootPositionWeight = 0;
    27.     [Range(0.0f, 1.0f)]
    28.     public float leftHandPositionWeight = 0;
    29.     [Range(0.0f, 1.0f)]
    30.     public float rightHandPositionWeight = 0;
    31.     [Range(0.0f, 1.0f)]
    32.     public float rightElbowHintWeight = 0;
    33.     [Range(0.0f, 1.0f)]
    34.     public float leftElbowHintWeight = 0;
    35.  
    36.     [Range(0.0f, 1.0f)]
    37.     public float leftFootRotationWeight = 0;
    38.     [Range(0.0f, 1.0f)]
    39.     public float rightFootRotationWeight = 0;
    40.     [Range(0.0f, 1.0f)]
    41.     public float leftHandRotationWeight = 0;
    42.     [Range(0.0f, 1.0f)]
    43.     public float rightHandRotationWeight = 0;
    44.     [Range(0.0f, 1.0f)]
    45.     public float rightKneeHintWeight = 0;
    46.     [Range(0.0f, 1.0f)]
    47.     public float leftKneeHintWeight = 0;
    48.  
    49.  
    50.     protected GameObject CreateIKGoal(string name)
    51.     {
    52.         GameObject obj =  GameObject.CreatePrimitive(PrimitiveType.Sphere);
    53.         obj.name = name;
    54.         obj.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    55.         return obj;
    56.     }
    57.  
    58.     // Use this for initialization
    59.     void Start () {
    60.         animator = GetComponent<Animator>();
    61.  
    62.         if (leftFootIKGoal == null)  leftFootIKGoal = CreateIKGoal("LeftFootGoal");
    63.         if (rightFootIKGoal == null) rightFootIKGoal = CreateIKGoal("RightFootGoal");
    64.         if (leftHandIKGoal == null) leftHandIKGoal = CreateIKGoal("LeftHandGoal");
    65.         if (rightHandIKGoal == null) rightHandIKGoal = CreateIKGoal("RightHandGoal");
    66.     }
    67.  
    68.     // Update is called once per frame
    69.     void Update () {
    70.         Debug.Log("EditorIK.Update()");
    71.         animator.Update(0);
    72.     }
    73.  
    74.     void OnAnimatorIK(int layerIndex)
    75.     {
    76.         Debug.Log("EditorIK.OnAnimatorIK()");
    77.         animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, leftFootPositionWeight);
    78.         animator.SetIKHintPositionWeight (AvatarIKHint.LeftKnee, leftKneeHintWeight);
    79.         animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, rightFootPositionWeight);
    80.         animator.SetIKHintPositionWeight (AvatarIKHint.RightKnee, rightKneeHintWeight);
    81.         animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandPositionWeight);
    82.         animator.SetIKHintPositionWeight (AvatarIKHint.LeftElbow, leftElbowHintWeight);
    83.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandPositionWeight);
    84.         animator.SetIKHintPositionWeight (AvatarIKHint.RightElbow, rightElbowHintWeight);
    85.  
    86.         animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, leftFootRotationWeight);
    87.         animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, rightFootRotationWeight);
    88.         animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandRotationWeight);
    89.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandRotationWeight);
    90.  
    91.         if (enabledIK)
    92.         {
    93.             animator.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.position);
    94.             animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.rotation);
    95.  
    96.             animator.SetIKPosition(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.position);
    97.             animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.rotation);
    98.  
    99.             animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.position);
    100.             animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.rotation);
    101.  
    102.             animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandIKGoal.transform.position);
    103.             animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandIKGoal.transform.rotation);
    104.         }
    105.         else
    106.         {
    107.             leftFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
    108.             leftFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftFoot);
    109.  
    110.             rightFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightFoot);
    111.             rightFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightFoot);
    112.  
    113.             leftHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftHand);
    114.             leftHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftHand);
    115.  
    116.             rightHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightHand);
    117.             rightHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightHand);
    118.         }
    119.     }
    120. }
    121.  
    Thank you very much for any clarification you can offer. Mecanim rocks my face off. I can finally put together the procedural IK kata rig training system I developed over a few years in C4D in Unity and will be releasing it to the Asset Store soon.
     
    Last edited: Jun 12, 2015
  11. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes, this is the only step needed to make it work.

    For IK hint you also need to send their position/rotation like other IK goal when IK is active
    http://docs.unity3d.com/ScriptReference/Animator.SetIKHintPosition.html

    And read their position from the animator when ik is not active. if you need some some feedback
    http://docs.unity3d.com/ScriptReference/Animator.GetIKHintPosition.html

    If you push the concept a little bit more you could even have a controller with some animation clip playing and with the live feeback when IK is not active you will see the current position of IK goal and Hint as computed by the animator for the currently playing clip.

    As for the creation of the IK goal and IK hint, you can do as you want, if you want to use other game object simply remove the create function and setup your gameobject in the inspector like you would do to setup reference for your game script
     
    theANMATOR2b likes this.
  12. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850

    I have revised the script to reflect the addition of IK Hints and got the foot IK goals in my rigs hierarchy to work. The KneeHints move the knees now when I rotate the foot IK goals of which they are children as per my setup of the past 13 years creating IK rigs.

    However I cannot move the elbowHint positions using the Position Handles in scene view though they do move to the positions I set in script in game mode when playing. Same with As well..I made special proxies so I did not have to set scaling which originally was at 0.1 (using default spheres). Now when I turn their scale to 1 when the EditorIK script is off they immediately revert back to 0.1 when the script is turned back on... What the heck.. Makes no sense. I have even reset the script. Is it holding onto this scaling somewhere? The relevant code is as follows ...

    Code (CSharp):
    1. void OnAnimatorIK(int layerIndex)
    2.     {
    3.         Debug.Log("EditorIK.OnAnimatorIK()");
    4.         animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, leftFootPositionWeight);
    5.         animator.SetIKHintPositionWeight (AvatarIKHint.LeftKnee, leftKneeHintWeight);
    6.         animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, rightFootPositionWeight);
    7.         animator.SetIKHintPositionWeight (AvatarIKHint.RightKnee, rightKneeHintWeight);
    8.         animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandPositionWeight);
    9.         animator.SetIKHintPositionWeight (AvatarIKHint.LeftElbow, leftElbowHintWeight);
    10.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandPositionWeight);
    11.         animator.SetIKHintPositionWeight (AvatarIKHint.RightElbow, rightElbowHintWeight);
    12.  
    13.         animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, leftFootRotationWeight);
    14.         animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, rightFootRotationWeight);
    15.         animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandRotationWeight);
    16.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandRotationWeight);
    17.  
    18.         if (enabledIK)
    19.         {
    20.          
    21.             animator.SetIKHintPosition(AvatarIKHint.LeftKnee, leftKneeHint.transform.position);
    22.             animator.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.position);
    23.             animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.rotation);
    24.          
    25.             animator.SetIKHintPosition(AvatarIKHint.RightKnee, rightKneeHint.transform.position);
    26.             animator.SetIKPosition(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.position);
    27.             animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.rotation);
    28.  
    29.             animator.SetIKHintPosition(AvatarIKHint.LeftElbow, leftElbowHint.transform.position);
    30.             animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.position);
    31.             animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.rotation);
    32.  
    33.             animator.SetIKHintPosition(AvatarIKHint.RightElbow, rightElbowHint.transform.position);
    34.             animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandIKGoal.transform.position);
    35.             animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandIKGoal.transform.rotation);
    36.         }
    37.         else
    38.         {
    39.             leftKneeHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.LeftKnee);
    40.             leftFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
    41.             leftFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftFoot);
    42.  
    43.             rightKneeHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.RightKnee);
    44.             rightFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightFoot);
    45.             rightFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightFoot);
    46.  
    47.             leftElbowHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.LeftElbow);
    48.             leftHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftHand);
    49.             leftHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftHand);
    50.  
    51.             rightElbowHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.RightElbow);
    52.             rightHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightHand);
    53.             rightHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightHand);
    54.         }
    55.     }
    56. }
    57.  
     
    Last edited: Jun 12, 2015
  13. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Just did a test and IK hint seem to behave normally in 5.1, here the code just in case.
    I'm not sure I understand exactly what is not working. You cannot manipulate elbow IK hint at all? that doesn't make any sense if nobody is actually writing values into thoses transform.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class EditorIK : MonoBehaviour {
    6.  
    7.     Animator animator;
    8.  
    9.     public bool enabledIK = false;
    10.  
    11.     public GameObject leftFootIKGoal = null;
    12.     public GameObject rightFootIKGoal = null;
    13.     public GameObject leftHandIKGoal = null;
    14.     public GameObject rightHandIKGoal = null;
    15.  
    16.     public GameObject leftKneeIKHint = null;
    17.     public GameObject rightKneeIKHint = null;
    18.     public GameObject leftElbowIKHint = null;
    19.     public GameObject rightElbowIKHint = null;
    20.  
    21.     public float leftFootPositionWeight = 0;
    22.     public float rightFootPositionWeight = 0;
    23.     public float leftHandPositionWeight = 0;
    24.     public float rightHandPositionWeight = 0;
    25.  
    26.     public float leftKneePositionWeight = 0;
    27.     public float rightKneePositionWeight = 0;
    28.     public float leftElbowPositionWeight = 0;
    29.     public float rightElbowPositionWeight = 0;
    30.  
    31.     public float leftFootRotationWeight = 0;
    32.     public float rightFootRotationWeight = 0;
    33.     public float leftHandRotationWeight = 0;
    34.     public float rightHandRotationWeight = 0;
    35.  
    36.  
    37.     protected GameObject CreateIKGoal(string name)
    38.     {
    39.         GameObject obj =  GameObject.CreatePrimitive(PrimitiveType.Sphere);
    40.         obj.name = name;
    41.         obj.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    42.         return obj;
    43.     }
    44.  
    45.     // Use this for initialization
    46.     void Start () {
    47.         animator = GetComponent<Animator>();
    48.  
    49.         if (leftFootIKGoal == null)  leftFootIKGoal = CreateIKGoal("LeftFootGoal");
    50.         if (rightFootIKGoal == null) rightFootIKGoal = CreateIKGoal("RightFootGoal");
    51.         if (leftHandIKGoal == null) leftHandIKGoal = CreateIKGoal("LeftHandGoal");
    52.         if (rightHandIKGoal == null) rightHandIKGoal = CreateIKGoal("RightHandGoal");
    53.  
    54.         if (leftKneeIKHint == null) leftKneeIKHint = CreateIKGoal("LeftKneeIKHint");
    55.         if (rightKneeIKHint == null) rightKneeIKHint = CreateIKGoal("RightKneeIKHint");
    56.         if (leftElbowIKHint == null) leftElbowIKHint = CreateIKGoal("LeftElbowIKHint");
    57.         if (rightElbowIKHint == null) rightElbowIKHint = CreateIKGoal("RightElbowIKHint");
    58.     }
    59.    
    60.     // Update is called once per frame
    61.     void Update () {
    62.         animator.Update(0);
    63.     }
    64.  
    65.     void OnAnimatorIK(int layerIndex)
    66.     {
    67.         animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, leftFootPositionWeight);
    68.         animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, rightFootPositionWeight);
    69.         animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandPositionWeight);
    70.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandPositionWeight);
    71.  
    72.         animator.SetIKHintPositionWeight(AvatarIKHint.LeftKnee, leftKneePositionWeight);
    73.         animator.SetIKHintPositionWeight(AvatarIKHint.RightKnee, rightKneePositionWeight);
    74.         animator.SetIKHintPositionWeight(AvatarIKHint.LeftElbow, leftElbowPositionWeight);
    75.         animator.SetIKHintPositionWeight(AvatarIKHint.RightElbow, rightElbowPositionWeight);
    76.  
    77.         animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, leftFootRotationWeight);
    78.         animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, rightFootRotationWeight);
    79.         animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandRotationWeight);
    80.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandRotationWeight);
    81.  
    82.         if (enabledIK)
    83.         {
    84.             animator.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.position);
    85.             animator.SetIKHintPosition(AvatarIKHint.LeftKnee, leftKneeIKHint.transform.position);
    86.             animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootIKGoal.transform.rotation);
    87.  
    88.             animator.SetIKPosition(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.position);
    89.             animator.SetIKHintPosition(AvatarIKHint.RightKnee, rightKneeIKHint.transform.position);
    90.             animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootIKGoal.transform.rotation);
    91.  
    92.             animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.position);
    93.             animator.SetIKHintPosition(AvatarIKHint.LeftElbow, leftElbowIKHint.transform.position);
    94.             animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandIKGoal.transform.rotation);
    95.  
    96.             animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandIKGoal.transform.position);
    97.             animator.SetIKHintPosition(AvatarIKHint.RightElbow, rightElbowIKHint.transform.position);
    98.             animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandIKGoal.transform.rotation);
    99.         }
    100.         else
    101.         {
    102.             leftFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
    103.             leftKneeIKHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.LeftKnee);
    104.             leftFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftFoot);
    105.  
    106.             rightFootIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightFoot);
    107.             rightKneeIKHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.RightKnee);
    108.             rightFootIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightFoot);
    109.  
    110.             leftHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.LeftHand);
    111.             leftElbowIKHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.LeftElbow);
    112.             leftHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.LeftHand);
    113.  
    114.             rightHandIKGoal.transform.position = animator.GetIKPosition(AvatarIKGoal.RightHand);
    115.             rightElbowIKHint.transform.position = animator.GetIKHintPosition(AvatarIKHint.RightElbow);
    116.             rightHandIKGoal.transform.rotation = animator.GetIKRotation(AvatarIKGoal.RightHand);
    117.         }
    118.     }
    119. }
    120.  
     
    avidwriter, muzboz and WaqasGameDev like this.
  14. riccardotognin

    riccardotognin

    Joined:
    Nov 12, 2012
    Posts:
    4
    @Mecanim.Dev Based on your sample codes i'm building a solution for making small corrections to any Mocap animation applied to a Humanoid right in Editor. I create a sort of "shadow rig" (actually i create 4 "shadow limbs") that moves with the actual mocap data and allows you to move IKgoals and IKhints (which are children of these shadow limbs) in a sort of local limb space.

    The goal of this is to fix hands compenetrations with the body during standing idles, for example, or incorrectly placed feet on the ground... In short terms, i compensate erroneous animations with an animation-delta (which can also be animated during clips) using the Mecanim IK solver.

    I'm already at a very good point, but i just stumbled against a (i hope not major) issue: since my code works almost entirely in the OnAnimatorIK callback i have problems to see it working when I move the timeline in the Editor to preview just one animation from the animator (which can be a very complex state machine). Since I'm calling animator.Update(0) basically at any Update function of a ExecuteInEditMode script, it completely breaks the preview of the animation, resulting in just the 1st frame being displayed (and making my script almost useless in edit mode). I also tried removing the forced animator update, hoping that, when you move the timeline, an actual animator.Update call is raised from the timeline, but with no luck: there is no OnAnimatorIK callbacks during animation preview in editor (I'm still not sure why).

    At the end i can only use my code in runtime, pausing the game when I need to making corrections, moving goals or hints without actually seeing the result live (I'm still in pause...), resuming (hoping that the correction was enough and not too much), and then, when it's everything looking good, copying the values of the component and re-pasting them when i stop the game...

    It may be sound too preposterous asking for any advice but, any advice? :)
     
  15. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes this is the expected behaviour when you move the animation window timeline. The animation window use a Sampling function which create a custom controller with only one clip to avoid having weird behaviour when you key frame a clip, it would not make any sense in the animation window context to have a transition triggered while playing one animation clip.
    http://docs.unity3d.com/ScriptReference/AnimationMode.SampleAnimationClip.html

    In this case I think you will need to create your own keyframing tools which should sample your complete controller with animator.Play() to set the current time and animator.Update(0) to force the update
     
    riccardotognin likes this.
  16. riccardotognin

    riccardotognin

    Joined:
    Nov 12, 2012
    Posts:
    4
    Thank you for your explanation. Now I totally understand the behaviour of the Animation window. It just create an overriding controller that play a single clip "as is", basically taking values from the clip and only assigning them without any consideration for layers or other things that can be stacked in runtime.

    It seems really obvious now, the only way to go is to create a sort of "runtime timeline" in editor to see the full stack of animations and IK layers evaluated.

    Thank you again!
     
  17. tinyant

    tinyant

    Joined:
    Aug 28, 2015
    Posts:
    127
    Waiting for Unity3d's Animation Job system.
     
  18. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    I ran into a small issue with that technique. The IK script works fine in the Editor now but when I do a change and I save the scene, it stops triggering the OnAnimatorIK() function for some reason. The Update function still triggers like usual but it looks like the "animator.Update(0)" stops to work... Any clue on why is that and any fix?
     
  19. nukadelic

    nukadelic

    Joined:
    Aug 5, 2017
    Posts:
    73
    @danUnity this worked for me:
    Code (CSharp):
    1.  
    2.         animator.Update( 0 );
    3.         script.transform.position = script.transform.position + Vector3.one;
    4.         script.transform.position = script.transform.position - Vector3.one;
    5.  
     
  20. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    @VVad not sure what you are doing there. You are moving up and down to the exact same location? How can you even know it works or not?
     
  21. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    Make sure that you add [ExecuteInEditMode] .