top of page
Writer's picturesantosh nalla

Applications of Inverse Kinematics in Unity- Picking Up Objects

In my previous blogs i have explained about Unity animations and Inverse Kinematics. Today lets see some applications of inverse kinematics in unity.

There are lot of in game applications of Inverse Kinematics in unity.

Lets suppose you need to pickup health pack, ammunition, guns etc in your game. Yes you might have that pickup animation to play, but how do you make sure your hand reaches that pickup object? What ever angle you are related to pickUp object, you always have to make sure your hand reaches that object. For this we use Inverse Kinematics for the pick up hand(lets suppose right hand in this case).

Result:


Before understanding how can we achieve this result, you need to understand about "Curves" in animations.

If you select any humanoid animation and go to the animation tab, you can find "Curves". What is this exactly and what is the exact use. Suppose you want to modify value of any float variable through out that particular animation, we use curves. there you define the float variable which you want to modify and also define curve- which defines how that variable is changed while that particular animation is played.


in the above example- "_pickUpWeight" is the float variable i have defined in the Animator window parameters - For pick up animation. I have also defined the curve to show how to change that variable value while we play this pickup animation.


Now lets define the logic. As soon as the character comes to the pickup objects trigger area, we should be able to press P and play pick up animation. Here, also i made sure that the angle between the pickup object and character is not greater than 45 degrees.


 private void OnTriggerStay(Collider other)
    { 
    if (other.tag == "PickUp")
       {
            pickUpObject = other.gameObject;
            if (Vector3.Angle(transform.forward, other.transform.forward) < 45)
            {
                if (Input.GetKeyDown(KeyCode.P))
                {
                    _animator.SetTrigger("_pickUp");
                    StartCoroutine(pickUp_Ienumerator(other.gameObject));
                }
            }

            if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Picking Up"))
                _characterState = CharacterStates.Picking;
            else
                _characterState = CharacterStates.Locomotion;
        }

 IEnumerator pickUp_Ienumerator(GameObject _pickUpObject)
    {
        yield return new WaitForSeconds(1.5f);
        _pickUpObject.transform.SetParent(_pickUpParentPart);
        _pickUpObject.transform.localPosition = new Vector3(-0.072f, -0.084f, -0.088f);
        _pickUpObject.transform.localRotation = Quaternion.Euler(-47.139f, 142.502f, 48.267f);
        yield return new WaitForSeconds(3f);
        Destroy(pickUpObject);
    }

This pickUp_Inenumerator makes sure the pickup object sticks to our hand.


Now our next job is to make sure our hand goes in the direction of that pickup object. For that we use Inverse Kinematics.


  private void OnAnimatorIK(int layerIndex)
    {
        if (pickUpObject)
        {
            //For PickUp
            _animator.SetIKPositionWeight(AvatarIKGoal.RightHand, _animator.GetFloat("_pickUpWeight"));
            _animator.SetIKPosition(AvatarIKGoal.RightHand, pickUpObject.transform.position);
        }
    }

Here if we observe we are using _pickUpWeight value which we have set up using curves to add weight to our position IK , so that our character hand doesn't always point the pickup object.


So that is it!! This is how it can be achieved.

These are many other applications of this Inverse Kinematics in Unity.

One of the very popular application is making sure your feet are aliened to the terrain the character is walking. You can try that for yourself using this same concept.

139 views0 comments

Recent Posts

See All

Commentaires


bottom of page