IK in Unity (reverse dynamics)

Reverse dynamics, in short, is to make a part of the character (face, left and right hands, legs) face a certain position. Here, we need to pay attention to several points
1: The animation of a character must be a human ID (this can be found by selecting the model - rig - AnimationType)
2: Open window - > animator in the Unity navigation menu bar to open the animation controller window, where IK Pass must be checked!!!

Drag the following code onto the character:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// IK
/// </summary>
public class Test7 : MonoBehaviour {

    /*
     * The motion of the parent driven by the child is called forward dynamics
     * The motion of the parent driven by the child is called inverse dynamics IK
     */
    Animator anim;
    public Transform Head;//Fake sub object head
    public Transform Left;//Pretending to be a child object left hand
    public bool IsTrue = false;
    void Start () {
        anim = this.GetComponent<Animator>();        
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
            IsTrue = true;
        if (Input.GetKeyDown(KeyCode.Z))
            IsTrue = false;
    }

    // IK specific functions
    //It is a callback method.
    //The premise is to open the window - > animator in the Unity navigation menu bar to open the animation controller window, where IK Pass must be checked!!!
    private void OnAnimatorIK()
    {
        if (anim)
        {
            if (IsTrue)
            {
                if (Head != null&& Head.gameObject.tag=="Player")
                {
                    anim.SetLookAtWeight(1); //Weight value
                    anim.SetLookAtPosition(Head.position);  //The object the head wants to stare at   

                }
                else
                {
                    anim.SetLookAtWeight(0);   //If the weight value is set to 0, it won't be seen
                    //You can test it in the panel, change the ball label on the head, and the main player who moves the head doesn't look at him      
                }

                if (Left != null)
               {
                    anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1); //Set the weight value of the left hand to 1
                    anim.SetIKPosition(AvatarIKGoal.LeftHand, Left.position); //Let the left hand of the model follow the left ball

                    ///Set the weight of the bone, 1 for the complete bone, if 0.5 So the bone weight is half, and the movable or rotatable one is half. You can modify the parameters to try.
                    anim.SetIKRotationWeight(AvatarIKGoal.LeftHand,1);
                    anim.SetIKRotation(AvatarIKGoal.LeftHand, Left.rotation);
                }
                ////If cancelled IK Animation, which resets the coordinates of the bones.
                else
                {
                    anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, 0f); //Let the weight value of the position be 0
                    anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0f); //Let the weight value of rotation be 0
                }
            }
        }
    }
}

Picture in the scene:

Then we can test it.

Keywords: Unity

Added by monkeypaw201 on Fri, 03 Jan 2020 04:24:37 +0200