In this article, I am going to talk about using Inverse Kinematics in Unity and in particular characters looking at each other.
If you have ever played games like Uncharted 4 or Final Fantasy XV, when characters talk to each other whilst the player is controlling them, you will see them physically look at each other.
This to me is really cool and adds a level of immersion that you wouldn’t get if the characters “just” talked to each other.
So firstly what is IK?
Well in simple terms, it is an animation technique.
Most animation is produced using a technique called forward kinematics. This is where an animation is created by rotating the angles of joints in a skeleton to predetermined values. The position of a child joint changes according to the rotation of the parent and thus the end point of a chain of joints can be determined from the angles and relative positions of the individual joints it contains.
Inverse Kinematics is working backwards to forward kinematics (hence the name!). By taking a chosen point in game space, you work backwards and find a valid way of orientating the joints so the endpoint lands at that position.
In simple terms, it allows us to make characters do stuff like point at things, look at things, touch an object at a specific point, etc in a more dynamic way. Unity actually has this built into Mecanim, which is pretty sick.
In order to get IK working you need a “correctly configured avatar”. In order to get one easily, I grabbed the Ethan from the Standard Assets.
Then we go an make an animator controller with his idle animation and set it up for IK by clicking on the little settings icon and enabling IK pass.
Setting up the character to look at stuff is super easy. Create a new script called IK Controller and add the following:
Add a sphere into the scene and set it up as your _lookObj. Set up the other references. Run the game and move the sphere around a bit and viola.
There you go. Easy!
If you want a sample project all the code lives here: