IK Weapon System in Unity
First of all, let’s talk a bit about Inverse Kinematics IK
When working on animations, we may find something like this:
On the left we can see an articulated body, and on the right it’s Avatar’s rigging map
An articulated body is a tree made up of linked chains with links (13,11,9), joints (I,G,F) and a root joint (A)
Articulations and Poses
Articulation is the rotation of a joint, and that rotation will move the connected links.
Pose is a set of joint articulations which result in positioning the articulated body
Forward Kinematics
The Forward Kinematics function/algorithm:
- Takes the whole pose as the INPUT (A,B,C,1,2,3)
- Calculates the position of the end effector (E) as the OUTPUT
This means that you need to define the articulation of each joint in the articulated body. It is ok for a low number of joints, but not for a high number
Inverse Kinematics
Let’s say we know the target position T that we want the end effector E to reach (left image), but as you may notice, for reaching T, the rest of the pose needs to be modified.
Inverse Kinematics is the inverse function/algorithm of Forward Kinematics. IK:
- Takes a target position (T) as the INPUT
- CALCULATES the pose required (A,B,C,1,2,3) for the end effector (E) to reach the target position T
So, the POSE (A,B,C,1,2,3) is the OUTPUT
Weapon with IK
- Add a Rig Builder component in the root gameobject that contains the Animator component
- Create an empty gameobject called IK and add a Rig component to it
- Add IK to the Rig Layers in Rig Builder
- Add a Two Bone IK component to L_Hand and add the gameobjects from hierarchy to inspector
Now let’s configure additional settings
Target will be the place I want the hand to move towards to. Weight 0 is original position, and 1 is target position
Hint will be position the joint (Left forearm) points to.
Do the same for Right Hand
Create 2 empty gameobjects in the Gun gameobject (USP) called Hand_Position_L and Hand_Position_R
The desired behavior is that L_Hand gameobject always go where the Hands_Position_L is
Attach the SmoothDamp
script to L_Hand. Do the same for R_Hand
Modify the Hand_Position_L and Hand_Position_R values in Play mode, copy the component values, exit play mode and paste those values into the same gameobject.
The goal is the character to look some like this in play mode.
I want to thank Luis Bermudez because the first part of this post is based on his blog Overview of Inverse Kinematics