Unity Triple A – Characters looking at stuff via IK

I have been talking about doing this for ages, and as my interest for Triple A techniques increases and the blog was looking a little sparse, I thought I would do a few tutorials on more Triple-A techniques. 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. If you look at the gif above you can see a transform being moved around the environment and the characters head looking at it, whilst the rest of his body is still. That is the sort of thing we will be looking at in this article.

So firstly what is IK?

Well in simple game dev 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.

Then make sure your animator controller is actually assigned to your character’s animator.

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.

We have a system similar to what is shown above.

Now in conversations, you can do a distance check if the characters are close enough you can enable the IK look at.

There you go. Easy!

If you want a sample project all the code lives here:

https://github.com/coxlin/IKTest

Overriding clips in the animator through code

Unity did a nice post recently on Recore’s animation system with the layered state machine, which reminded me of something cool I have done in games before and something cool I am doing int my personal projects.

Before we get started you should read up on:

Awesome, OK here is the scenario. Say you want are making a JRPG like I am Setsuna (pictured above) and say for each ability you want the character toperforma a different character animation. Not one per character, a different character animation per ability. It would be pretty mental to set up a layer per ability they can use. Also what if you were making one of those fancy games-as-a-service and wanted to add cool abilities later on without doing a binary update. Here is one approach.

Firstly I have created this basic state machine for the characters (notice there are no transitions)

capture

And I have created the corresponding override controller that has the default clip in the use ability slot

capture2

We are going to override this in code using the following utility function:

code

Now if you run the animator controller and the name of the state, and the clip you want the Use Ability state to play into this function, Unity will play the clip you loaded in!

Neat huh!