Just Looking Around

July 10, 2009 in Technology

Just making characters in your game look around can bring them much more to life as well as express important information to the user. Here we’ll discuss a few use cases and present a script that makes it simple to implement in your game.

First a tech demo video to set the context:

[vimeo clip_id="5538153" width="640" height="480"]

As can be seen, the looking behavior works regardless of which animations are playing. (One problem though is that when the two hands are holding the gun together, they are not always properly coordinated, but that’s a problem to solve another time!)

Use Cases
When two characters in a game are talking together (one of which might be a player avatar) they can look at each other to make the interaction look more sincere.

When the avatar comes near a health pack, a weapon that can be picked up, or another important item, he could look at that item. Besides looking natural, this can also make the player more attentive to the item if she hadn’t noticed it herself.

When there’s nothing of particular interest nearby, simply making the character look around at different random points in space looks much more natural than just looking straight ahead. It can also express mood – if the character looks around only at points directly in front, he is probably pretty focused, while if he looks around to the sides and behind him too, he is probably more exploratory or searching around.

Head Look Controller
In between the main things we’re working one here at Unity, we sometimes get to do some projects on the side. In order to make it simple to make a character look at any point in space without requiring extra animations, I have made a head look controller script component as such a side project:

Head Look Controller example project

Head Look Controller (script only) at Unify Community Wiki

This script can be used to make a character look towards a specified point in space, smoothly turning towards it with for example the eyes, head, upper body, or whatever is specified. Multiple ranges of bones can be specified, each with different settings for responsiveness, angle constraints etc.

In the video above the controller on the character is set up with one range of bones for the spine and one for the neck and head. Unfortunately the eyes of that character can’t turn, but that could be set up as well with an appropriately rigged character.

Segments should always be specified with the bones/transforms closest to the root specified first and the ones deeper in the skeleton specified after that.

Here’s the settings I used in for the component:

characterlookatcontroller

I have set the Threshold Angle Difference for the spine to 30, since the spine shouldn’t turn if the target angle (the angle between the direction towards the target and the direction straight ahead) is less than 30 degrees. The Bending Multiplier control how much the segment bends or turns compared to how big the target angle is. The segment will attempt to be no more than Max Angle Difference away from the target angle, though it won’t bend any further once it reaches the Max Bending Angle.

I set the responsiveness of the head segment higher than that of the upper body, since people tend to turn the head quicker. If segments had been specified for the eyes as well, they should have had an even higher responsiveness.

I specified the left and right arm under Non Affected Joints, so that the arms keep keep pointing in the original direction despite the bending and turning of the upper body. By setting the Effect to 0.3 I let them be affected a little bit though.

The Head Look Vector and Head Up Vector are just used for reference to know the default alignment of the head. Per default they are set to forward (0,0,1) and up (0,1,0) respectively.

The Target point that the character should look at is specified through scripting.

“Pro Tips!”
I said above that a character could look around at random points when there’s nothing else to look at. Actually, looking around at a few different points looks more natural in any case. Even when talking to a person or when having found an important object, people rarely tend to stare incessantly at the same point. Introduce more variation by looking at the main point of interest most of the time, but look away at some random points in space now and then.

And if your character supports it, don’t forget to blink once in a while! That is super-easy to do, either with a procedural script or with a traditional animation that only affects the eye-lids and which plays all the time at a higher layer than the other animations. Blinking a bit can really help make a character look less artificial, so if the eyes of a character are ever seen in your game, it’s worth spending the small amount of time it takes to implement.

Go on! Make those characters more believable!

Comments (12)

Subscribe to comments
  1. Frank Ramirez

    October 1, 2011 at 12:59 pm / 

    Thanks so much for this Rune. I used this script in our Unity iOS Game: Dragon Eggs. I used it to make the mama dragon look at dangerous obstacles when nearby and at the baby egg when a lower .effect value if there weren’t any. This script really helped bring the character to life. Everyone was so amazed the first time I ran this script.

    Also because our dragon model node was rotated slightly i had to put this code at the top of HeadLookController.Start()
    Vector3 lookVector = Vector3.forward;
    headUpVector = rootNode.InverseTransformDirection(Vector3.up);
    headLookVector = rootNode.InverseTransformDirection(lookVector);

    Dragon Eggs Trailer http://goo.gl/HrSOH
    Dragon Eggs iPhone http://goo.gl/vSVLY
    Dragon Eggs iPad http://goo.gl/j7LPk

  2. Rune Skovbo Johansen

    July 25, 2011 at 8:32 am / 

    @Michael: The Head Look Controller was not made for being used for a first person camera. Nevertheless you may get better responsiveness by tweaking the Responsiveness variables in the component.

  3. Michael

    July 18, 2011 at 1:47 am / 

    I hope I will get an answer as this was first posted 2 years ago.
    I’m making a first person rpg at the moment. I was trying to make it so player can see characters whole body while moving camera around, so it’s not only pair of hands glued to the main camera. At first I just sticked camera under one of head points (I’m temporarily using Hero model) and scripted it so it was responding to mouse moves at Y axis. It worked when placed properly but had 2 flaws while moving – sometimes I saw the model from the inside due to animations + the camera was shaking and making me nauseous.

    Then I found your script and thought a way to use it. I used the ball (without any graphics) glued to main character object, made it go up and down from -0.1f to 3f while the mouse is moving on Y axis. Then I copied the whole spine + neck + head – without the animations and model graphics and added a camera. In the result camera acts as it should – stares at invisible mouse-controlled ball and is not affected by animations – though player may see his character moving.

    The only problem is – and I don’t know both unity and C# as well to found it out myself – that there is this little lag between mouse move and head/camera move. While it’s absolutely realistic I don’t like it. I’d rather want the look to be fluent like in FPS games.
    If you’re reading this and can help me OR I made something unclear – please respond to my email. Thanks in advance!

  4. kranthi

    January 6, 2010 at 1:02 pm / 

    Thanks, it’s really very helpful for me

  5. Pat AfterMoon

    July 13, 2009 at 11:17 pm / 

    Thanks you so much, it helps me a lot!

  6. Rune Skovbo Johansen

    July 13, 2009 at 2:30 pm / 

    @ Pat AfterMoon: I updated the script on the Wiki so it has an option to be used even when there is no animation driving the bones that the script modifies. I hope that helps!

  7. n0mad

    July 12, 2009 at 11:09 pm / 

    Now all of these resources are making the whole thing more and more impressing …

  8. Pat AfterMoon

    July 12, 2009 at 9:35 pm / 

    Thank you, it’s a nice resource.
    But I have troubles with it. Like mentioned in the wiki, the character needs to have animations that drive all the bones that the script modifies. I use a model that has no animation for this part of his body. Only his legs, bust and arms are animated.

    Is there something I can do by script to prevent the body parts to turn or twist around out of control?

  9. Nikko

    July 12, 2009 at 3:45 am / 

    Wow very nice! Thanks!

  10. Twitchfactor

    July 10, 2009 at 9:28 pm / 

    Wow! I’ve been making 3rd person action/adventure games for A LOT of years and that’s some really nice head tracking!

  11. David Goldfinch

    July 10, 2009 at 4:52 pm / 

    Dude, that is so awesome! That is going to be a useful one ..

  12. Minevr

    July 10, 2009 at 4:38 pm / 

    Haaaa~~Handsome

Comments are closed.