First of all, if you don’t know the meaning of inverse kinematics, here is the definition:
Inverse kinematics simplifies the process of animating jointed / segmented figures by making the motion of each part related to the motion of the linked parts. That way you simply have to animate the starting and ending joins, and the ones in between will adjust themselves according to the physics programming and create more natural looking movement. In forward kinematics, each piece would have to be animated separately.
Simple IK is a very handy script that allows you to add inverse kinematics to some bones and joints. After downloading the Simple IK asset, the script is available inside the folder Takohi with the name SimpleIKSolver.cs.
The aim of this article is to add inverse kinematics to an arm of a rigged humanoid model. The model we use can be downloaded for free here.
Our goal is to make the avatar touching a sphere with his right hand by always keeping a realistic human posture.
Preparing the scene
First, import the avatar into the scene, then create a empty game object you call ‘Right Arm IK’. This game object can be located anywhere and will be used to hold the script.
Now we need to make a target. A target (any game object) will represent the location the inverse kinematics system will to try to reach. As we said before, we want our avatar to touch with his right arm a sphere, so add a new sphere into the scene and name it ‘Target’. Resize it to have approximately the size of a tennis ball and put it front of our character.
Configuring the IK
Now we will add the inverse kinematics to the right arm of the avatar. From the Takohi folder, drag’n drop the SimpleIKSolver.cs script on the ‘Right Arm IK’ game object.
Here is a description of all parameters:
- Is Active: enable IK solver
- Target: transform object to reach
- Join Entities: list of all joins composing the IK
- Join Entity:
- Joint: transform object of the joint
- Angle Restriction Range:
- X Axis, Y Axis, Z Axis: enable angle restriction on x/y/z axis
- X Min, Y Min, Z Min: minimum angle for x/y/z axis (from -180°)
- X Max, Y Max, Z Max: maximum angle for x/y/z axis (up to 180°)
- Is Damping: enable damping
- Damping Max: damping value
For the target, just drag and drop the sphere we called ‘Target’ into the Target property.
Next, we will assign all the joints composing the IK. If you look at the structure of the avatar by dropping down the list, you should find that the right arm has three joints: arm, forearm, and the hand.
In the inspector of ‘Right Arm IK’ game object, set the size of the Join Entities array to 3 and add consecutively these joints. Then enable damping and set a value to 0.05 in order to smooth the movement.
Everything is ready! Now play the scene and you should see the character trying to reach the target with his right hand.
As the picture below, according to the position of the sphere, the arm of the character will have an unrealistic posture that is not possible for a human. We would like to avoid this situation and make our avatar more realistic.
To avoid this kind of unrealistic movement, we will add angles restrictions: the same angles restrictions as a human arm. If you look at the arm below with the axis, you can try by yourself to know with your own arm and for each axis what is the angles restrictions imposed by your body.
After measuring with my own arm the angles limits for each axis and each joint, these are the values I set as angles restrictions to have a realistic movement of the arm:
By example for the right forearm joint, it can only rotate now from -20° to 20° around the x-axis, from -150° to 0° to the y-axis, and no rotation allowed on the z-axis.
Now play again the scene and move the sphere to different positions. The arm of your avatar will always keep a realistic posture.
Now you have your character that can move his right arm to reach any object on the scene. Of course, inverse kinematics are not only useful for moving arms and legs but can be used in many cases (crane, snake, …) and so our asset Simple IK. Just use your imagination!
If you have any question, please ask us here on the dedicated thread on the Unity forum. We will reply to you as quick as possible.