Nov 5th 2019
Inverse Kinematics allows you to modify any animation in runtime so you can make a specific joint to follow a target ignoring partially or totally the animation being played. In Unity3D 2019 there is a new and easy way to implement them. Let’s implement this:
When you play an animation in a mesh. The position of every joint is defined by the animation. This method is called forward kinematics. And forward kinematics sucks.
In forward kinematics, every joint moves the next one from the inside out. In humans, the spine moves the shoulder which moves the elbow which moves the wrist which moves the hand.
The problem with that makes evident when we want to make the hand to move to a specific position. To do that we need to define the hole animation from the spine to the hand. Even worse if the target position of the hand changes in runtime we are totally fucked.
Animations between characters like a simple handshake are a total pain to implement with forward kinematics.
When we don’t control the animation in runtime there is no chance the animated character can interact with any changing environment.
Just a little bump in the floor could make the character show wrong poses.
In animations, especially in humanoid, it is pretty useful to set the position of the hands or the foot in runtime.
Here is when Inverse Kinematics comes to the rescue.
IK allows you to define an animation in runtime from outside to inside. You can define for example to move the feet to the top of the small bump in the floor.
First, you have to download and install the Animation Rigging preview package.
Windows -> Package Manager
Advanced, make sure the
Show Preview Packages is enabled.
(It will take a while. Just be patient.)
Animation Rigging preview package and then install it at the bottom right corner of the Package window.
Setup your animated character as usual. In my case, I’ve made a Unity Project with a simple humanoid playing a looped walking animation.
If you want you can visualize any bones by adding a component called
BoneRenderer to the root of your character.
RigBuildercomponent to the root of your character
- Create an empty object inside the root of your character
Rigcomponent to that object and name it
- Add your
Rigcomponent to the
RigBuilderyou added first
- In your
Rigobject add a
- Assign the Root, Mid, and Tip
- Create a new GameObject inside your
RigGameObject and name it
- Set the target in the desired position
Feel free to download or clone the project here