- Home /
Move Blender model's head with LookAt
I'm not sure if this is a Blender issue or a Unity issue - apologies if I'm barking up the wrong tree.
My partner & I are making a simple little low-poly game; I'm making character models in Blender and he's programming in Unity. We want to have NPCs turn their heads and look at the first-person camera when the player interacts with them (ie, on mouse-click), but we're not sure how to best achieve that. I know I can animate a head-turn in Blender, but that won't account for all the positions from which the player might interact with the model (unless I make a bunch of tedious animations). I think ideally it would involve a LookAt function so they can dynamically target the player/camera, wherever they stand. I don't know how or if that would work with Blender rigging, though. There's lots of info on how fully animated Blender models can be used in Unity, but we really want just one part of the rig to move situationally.
Is a completely different method necessary? Or is there a workaround we're not aware of?
Answer by Eno-Khaon · Jan 01 at 07:59 PM
When you import a 3D model from Blender to Unity, it's not being used in Blender's file format(s) anymore. It's being processed to be compatible with Unity and doing everything in its power to ensure that there should be little to no difference from what you had created in Blender.
This includes all of the bones and bone weights on the vertices, for example. If the head "bone" is rotated, then it would be no different from rotating that bone in Blender.
Granted, there's generally a bit more nuance to the motion as you aim for a more "realistic" movement, so the more small, subtle movements are accounted for, the better it can look. For example, let's say you *DO* make a few "detailed" animations (look left, look hard left, left+up, left+down, etc.). Then, after those animations are factored in, the small differences can be made up by having the head "look at" the target (which, itself, is effectively the most basic form/starting point of Inverse Kinematics).
Answer by daotanp · Jan 02 at 08:04 AM
Something like this should works:
public Transform viewTarget
public bool canLookAtTarget;
//range from 0 to 1
public float totalWeight, bodyWeight, headWeight;
void OnAnimatorIK(int layerIndex)
{
if (canLookAtTarget)
{
animator.SetLookAtWeight(totalWeight, bodyWeight, headWeight);
animator.SetLookAtPosition(viewTarget.position);
}
}
Paste this block in any script, then attach the script to your NPCs. Assign what you want the NPCs to look at in the viewTarget field, adjust the weight as you want.
And remember to check the IK pass option in the animator controller layer, otherwise IK won't work.