Skip to content

Tutorials ‐ Character Head Tracking

Will Corby edited this page Sep 21, 2024 · 4 revisions

Character Head Tracking

When you import a custom character that you want your player to interact with, you'll probably want said character to look at the player. Fortunately, Unity's IK system and Mobius' own character animator component make it easy to do this, provided everything is set up correctly.

Note: I recommend confirming that head tracking works as early as possible in your workflow. If something goes wrong, depending on what it is, you could end up redoing a lot of work. For a fully custom character, I would do this as soon as you apply rigging to your character but before you fine-tune the weight painting.

Prerequisites

Unity's IK will only work on a humanoid character. Your character can have additional bones, but if your character is missing a limb, you may still need to create a fake limb so Unity registers your rig as a valid humanoid. You'll need the following bones (name does not matter, though it seems to help Unity to auto-assign the bones):

  • Hip (the base of the spine)
  • Spine (connected to the hip, there may be more spine bones between this and the neck)
  • Left and Right Upper Arms
  • Left and Right Lower Arms
  • Left and Right Hands
  • Left and Right Upper Legs
  • Left and Right Lower Legs
  • Left and Right Feet
  • Head

Many other bones can also be supported, but these are the absolute minimum needed.

Importing Your Character

Copy your character asset into your project if you haven't already. Click on the asset in the Project Window and check it in the Inspector. Go to the Rig tab.

image

By default, rigs are set to Generic. Set Animation Type to Humanoid. Leave Skin Weights at 4 Bones and Optimize Game Objects off. Hit Apply. If your character is set up correctly, you shouldn't get any errors, but if Unity complains you'll need to hit the Configure button. Set up all the bones in the pane on the right. You can ignore the Muscles and Settings tab.

image

Once you have all the bones set up, click Done in the Inspector which should close the Avatar Setup mode.

Whether or not you had to configure the avatar, you should now see the avatar within the character asset.

image

Setting Up IK

First, you need to create an Animator Controller asset for your character. Right-click the Project Window, Create > Animator Controller, then give it a name. Drag it onto your character, which should add an Animator component. Assign the Avatar you created earlier to the Avatar slot.

image

(Note: Your animator should never be on the top-level GameObject in your character, but rather the object that is the parent of both your rig root and the skinned mesh object. If that object is your top-level object, you should make another GameObject to be its parent.)

Open up the Animator Controller. This is most easily done by double-clicking the Animator Controller asset in the Project Window, but if the Animator Window doesn't automatically appear, you can show it by going to Window > Animation > Animator and then clicking on your Animator Controller.

image

Switch to the Layers tab and click the Cog by the Base Layer. Turn on IK Pass, which should put an IK indicator next to the Base Layer name. Ignore the other settings, including the Avatar Mask.

image

Making Head Tracking Work

Create a shape in front of the character. Mobius typically uses a Box Shape rotated 45 degrees. It should intersect the character. Give it an OW Trigger Volume.

image

image

Go to the object on your character that has the Animator component and add a Character Anim Controller. If your character has a Dialogue Tree somewhere on it, assign it to the Dialogue Tree field; otherwise leave it empty. Set the Player Tracking Zone to the trigger volume you created. For Look Spring, set Spring Constant to 50, Spring Coefficient to 14, and Mass to 1 (these numbers come from a base game character). You may need to tweak these if the IK feels off, but these settings should be good for the majority of cases. Turn off Look Only While Talking unless you already have dialogue set up and want that behavior. Head Tracking Weight should be left at 1, but you'll probably need to adjust that once you're testing in game. Set Blink Style to None unless you're using a base game Hearthian, in which case try different blink styles and settings. (If you want to set up blinking for custom characters, you'll need to set up Blend Shapes for them, but that is beyond the scope of this tutorial. I also haven't figured out how they work yet.) You can keep Skin Renderer blank, and Has Talk Animation should be off.

image

If this is all set up correctly, it should work now. Your character will look at the player when the player is in the trigger volume.

image

Note that there is a decent chance the character may look too far to the side. The easiest way to fix this is by reducing the Head Tracking Weight. Try a value of 0.5, and if that's too little, try 0.7. Keep tweaking the setting until it looks good.

It's not working!

The most likely reason it's not working is that a step was missed. Make sure:

  • The rig doesn't report any errors in the import settings
  • The rig is set as Humanoid
  • The Avatar is assigned to the Animator component
  • The Animator Controller has IK Pass turned on for its Base Layer
  • The Character Anim Controller component is on the same GameObject as the Animator component, and has its settings properly set
  • The Character Anim Controller has its Player Tracking Zone to a GameObject that both has a shape and an OW Trigger Volume

If these are all set, try putting a base game Hearthian animation in your Animator Controller (and set it as the Base Layer default animation). If the animation doesn't play, then your rig is likely corrupt and you'll have to remake it. If you're using Blender, you should use Rigify to generate your rig.