Just a heads up that I made a head look controller that you can use to make characters look at any point in space, turning their head and upper bodies etc. Blog entry (with video!): http://blogs.unity3d.com/2009/07/10/just-looking-around/ Example project: http://unity3d.com/support/resources/example-projects/head-look-controller If you want the script itself only, you can get it on the Unify Community Wiki: http://www.unifycommunity.com/wiki/index.php?title=HeadLookController Looking forward to see what you people can do with it. Rune
I tried a million times to write some code like yours, but I never really got it... Thanks, that code will save much of my time! (I need it for characters who should be able to shoot in different angles, and not only forward...)
very cool ! just a small suggestion: adding a "rootNode" public variable holding the root node transform, allows the script to be attached anywhere ( thus without breaking a Prefab ) - eg. to a global gamehandler... i made the following 3 changes to the class Code (csharp): public class HeadLookController : MonoBehaviour { // CHANGE 1 -> this declaration will hold the root transform to use public Transform rootNode; public BendingSegment[] segments; public NonAffectedJoints[] nonAffectedJoints; public Vector3 headLookVector = Vector3.forward; public Vector3 headUpVector = Vector3.up; public Vector3 target = Vector3.zero; void Start () { // Setup segments foreach (BendingSegment segment in segments) { Quaternion parentRot = segment.firstTransform.parent.rotation; Quaternion parentRotInv = Quaternion.Inverse(parentRot); segment.referenceLookDir = // CHANGE 2 -> here we use rootNode's transform parentRotInv * rootNode.transform.rotation * headLookVector.normalized; segment.referenceUpDir = // CHANGE 3 -> again here we use rootNode's transform parentRotInv * rootNode.transform.rotation * headUpVector.normalized; . . . .[continued] this small change works for me - now i can use the script anywhere, not just attached to the animated root bone....
Thanks, good point! I'll integrate those changes on the wiki next week (or you're welcome to do it if you want, as long as the changed script is fully tested and known to work). Rune
Hi, I tried and use your HeadLookController script and it works very good. So first of all thanks for the great script! I have a question though. I'm using the script on some npc characters that should look at the player when he gets close to it. When I get out of range I disable the script. All fine but the bone rotations are locked in the latest position when I disable the script. Shouldn't the animation take control over the bones when the script stops overriding it? It should at least snap back? //ohm
Yes it should. If the head doesn't snap back when you disable the HeadLookController component, it must be because you don't have any animations playing that have animation curves for the bones in question. Edit: Also see below for how to avoid snapping completely. Rune
I added an effect variable to the script on the Wiki that can be used to gradually turn the head look controller on and off. When effect is 1 the controller is fully turned on; when it's 0 it has no effect. Rune
Yes it was animation problems that I had before. It now works fine. I did a similar change as you did with the effect variable and interpolates from and to that before I turn on and off the script and it now works as intended. Thanks again! //ohm
I made a simple example project: http://unity3d.com/support/resources/example-projects/head-look-controller (It's a simplified version of the one shown in the video.) The script is the same as the one on the Wiki; the example project just provides a character to test the script on. Rune
Rune, this is really impressive, as is all your work. In fact, right now my player character is controlled by the Normal Character Motor and Platform Character Controller scripts, the Head Look Controller, and the Locomotion System! The character holds a flashlight that points at whatever surface is behind the cursor with the help of the Head Look Controller, and I'm using a modified version of the Follower script that changes only the desiredFacingDirection to get the body to point toward that same cursor-projected object while the actual movement is controlled with WASD. But I'm having a somewhat tangential problem. It's not a problem with the Head Look Controller per se, more a problem with the Normal Character Motor, but I figured it fit better here than anywhere else. Because the character motor's facing direction changes with the slightest angle change from the cursor object and at a constant speed, it looks rather stiff and nullifies most of the nice natural effect the Head Look Controller gave to controlling the flashlight. Worse, if I do move the cursor far and fast enough to see the character's arm significantly bend before the motor's facing direction catches up, when it does catch up the arm's gone too far and has to "snap back" to center on the cursor object which looks quite ugly and jarring. I thought I might be able to solve the first problem (the sudden, linear-feeling movement) by incorporating some ideas from the Head Look Controller into the character motor's UpdateFacingDirection function, such as a threshold angle difference and max angle difference. But solving the second problem (the "snap-back" on the Head Look Controller's actions) would seem to require making the Head Look Controller take the character motor's facing direction into account as if it were a segment in the Head Look Controller script itself, and though I've studied the scripts for the last day and a half I'm at a loss as to how to make it do so. Does anyone have any ideas? I'm not expecting anyone to do the work for me, just someone with more experience setting me on a track that might be right. Let me know if I'm not making sense and I'll try to clarify.
I have a vague understanding of the problem from your description, but seeing a video might help me better understand it. Rune
hopefully this helps: http://www.vimeo.com/6268716 i was a bit lazy/creative with the test scene textures, heh. i have someone else working on the real model and animation, which will be much more suited to holding a flashlight than the stock Hero model, but you should get the idea. it's worth noting that before i posted i tried many different combinations of settings in the head look controller and character motor rotations and the problems persist. actually without the follower script activated there is still a small amount of snap-back in the character's arm movements, but it's not nearly as jarring and exagerrated. but this may still be a problem that fine-tuning the head look controller might alleviate, rather than having to program something custom. as for the stiffness, i can't make the max rotation speed too small or it makes the character's reactions too sluggish and has a detrimental effect on gameplay. ideally the character itself should only start rotating once the angle of the target is a specified amount different enough, and it should only move about as far as it needs to for the flashlight to smoothly center on the target with the head look controller's help.
It sounds like you could make a check in the character motor if the current forward direction is less than a certain angle away from the desired direction, and if so, don't rotate further or only rotate very slowly. The threshold angle should match how far you have specified the arm to be able to point to the sides. Rune
this is the first thing i tried: Code (csharp): private void UpdateFacingDirection() { if (Vector3.Angle(desiredFacingDirection,transform.forward) < thresholdAngleDifference) return; which did stop the motor from turning instantly, but when it did move it was very choppy. i ended up adapting the features of the head look controller into UpdateFacingDirection, and it seems to work very nicely for my purposes. here's the changed code: Code (csharp): public float thresholdAngleDifference; public float turningMultiplier; public float maxAngleDifference; public float responsiveness; private void UpdateFacingDirection() { float angleDifference = Vector3.Angle(desiredFacingDirection,transform.forward); //handling threshold angle difference float hfacingThr = Mathf.Max( 0, Mathf.Abs(angleDifference) - thresholdAngleDifference ) * Mathf.Sign(angleDifference); //handling bending multiplier and max angle difference angleDifference = Mathf.Max( Mathf.Abs(hfacingThr) * Mathf.Abs(turningMultiplier), Mathf.Abs(angleDifference) - maxAngleDifference ) * Mathf.Sign(angleDifference) * Mathf.Sign(turningMultiplier); maxRotationSpeed = Mathf.Lerp( maxRotationSpeed, angleDifference, Time.deltaTime * responsiveness ); // Calculate which way character should be facing float facingWeight = desiredFacingDirection.magnitude; Vector3 combinedFacingDirection = ( transform.rotation * desiredMovementDirection * (1-facingWeight) + desiredFacingDirection * facingWeight ); combinedFacingDirection = Util.ProjectOntoPlane(combinedFacingDirection, transform.up); combinedFacingDirection = alignCorrection * combinedFacingDirection; if (combinedFacingDirection.sqrMagnitude > 0.01f) { Vector3 newForward = Util.ConstantSlerp( transform.forward, combinedFacingDirection, maxRotationSpeed*Time.deltaTime ); newForward = Util.ProjectOntoPlane(newForward, transform.up); //Debug.DrawLine(transform.position, transform.position+newForward, Color.yellow) Quaternion q = new Quaternion(); q.SetLookRotation(newForward, transform.up); transform.rotation = q; } } smoothing out the rotation like this probably isn't the right choice for many situations, but i thought posting it might be helpful to someone else using the Normal Character Motor down the road.
hmm, every so often, most commonly when waving the flashlight around slowly in front of the character, the character jerks violently for a split second and i get this warning: Code (csharp): Look rotation viewing vector is zero supposedly due to the following line: Code (csharp): q.SetLookRotation(newForward, transform.up); it looks like changing Code (csharp): maxRotationSpeed = Mathf.Lerp( maxRotationSpeed, angleDifference, Time.deltaTime * responsiveness ); to LerpAngle prevents that problem, but makes the rest of the movement choppy again. any idea what's up with this?
I'm trying to use the Head Look Controller to point the gun instead of the head. The obvious idea would be to have the character hold his arm out straight and point the gun wherever the mouse cursor is. However, anytime I put the arm/shoulder/hand/any combination of arm pieces, I can't get the gun to point at the cursor. Anyone have any luck with this?
I need more info. What does your animation look like without the Head Look Controller? Is he holding out his arm straight to begin with? How have you setup the Head Look Controller? Can you post a screenshot of it shown in the Inspector? When you say "I can't get the gun to point at the cursor" what exactly do you mean? What does happen? Do you get errors? Do the arm do something at all, or does it not respond to moving the cursor at all? Rune
This is absolutely amazing. Been banging my head against a wall, found this... Completely sorted. Thankyou!
Absolutely awesome component, but I have some questions regarding the customization of the head look controller for use with my project; I use the controller in conjunction with the aiming animation I have to create the control scheme that most modern games have, but since it raycasts a sphere into just about anywhere in the world when I get up close to an object the character starts looking at the top of the object, creating some rather strange looking animations... I noticed that in LookAtMouse script that the code creates a plane for the raycast to occur, so that prevents the issue that I mentioned. Is there a particular way to do this kind of thing? I'm using the package I found here: http://unity3d.com/support/resources/unity-extensions/head-look-controller And it's only available in C#, which I know nearly nothing about so I really cant make any modifications that I require... Can you help me out? Thanks in advance
Sorry to pound an old thread. But I'm trying to use the Javascript version of this script from the Wiki found here: http://www.unifycommunity.com/wiki/index.php?title=HeadLookController But it won't compile and the debug log says," (76,18) BCE0022: Cannot convert 'Object' to 'UnityEngine.Transform'., (188,18) BCE0022: Cannot convert 'Object' to 'UnityEngine.Transform'.
Hello, I'm encountering something similar to striderdoom. I've been using the head look controller to aim an arm. For testing purposes, i've set a single keyframe with the arm rigid and extended, to see if I can get it to aim exactly at the target. I've set the max angle difference as high as 360, the responsiveness in the 100's, and the max angle difference to 0. What I've noticed is that even when the target is in front of and near the aiming arm, it doesn't seem to target it exactly, but rather seems to simply aim in a more or less similar direction. As i pull the target farther away from the front of the arm, it seems responsive and closely follows the object, but if I draw a line from the end of the arm to the target, I find that although it's pretty close, it misses the target altogether. From the values that I've inputted I would have expected a perfect aim, even if it broke the model visually. In other words, what I'm asking is, is there a way to get the controller to aim at an object immediately and exactly? Am I going about this the wrong way? Thanks, Juanelo
Is this thread really abandoned by runevision? I really needed some help with this awesome tool! I wanted to use it along with AnimatePhysics on my model. But if i turn animatePhysics on it get really glitchy. Does anyone knows how to solve this? Thx! =) EDIT: Just talked to Rune himself, he said Unity currently doesn't support HeadLookController while using AnimatePhysics. So if you're having problem using it you may want to check if you got AnimatePhysics enabled, if you do, HeadLookController wont work properly.
Looks like a super awesome tool. Are there any tutorials? I can't seem to get it to work with my model. I attached the headlook controller script to my model, and the cursor hit to the sphere, and I set up segments and joints. Then when I hit play... nada. Any help with set up here would be greatly appreciated. -Richard
Has anyone been able to implement this with 3DMax's godawful orientation setup on biped characters? Cheers AC
Anyone got any ideas how I can get a smooth transiiton in and out of a target for this? I've got the head controller working fine, character looks at and tracks a target that is passed to the headcontroller script when the target enters a certain range. Trouble is, the head SNAPS to the target when the target is in range, then SNAPS back when i move the target out of range. I want a smooth transtion.
I have the same problem. I use : lookAtManager.effect = Mathf.Lerp(lookAtManager.effect, 1, 0.3f);" // in lookAtManager.effect = Mathf.Lerp(lookAtManager.effect, 0, 0.2f); " // out it can improve the result , but there still is "SNAPS" .Please help me
Necropost, but the fact that this is the main thread makes me feel less bad about it. I'd like to have this so that NPCs look at my character, but I can't figure out how I'd do that. The demo had a script on that ball that referred to a headLook variable within the headLookController, but that isn't an actual thing: Code (csharp): public HeadLookController headLook; void LateUpdate () { headLook.target = transform.position; I'm also using JavaScript, so I can't really dissect the code.
Oh in general I've had difficulty with the 3drt "seniors". I cant even remember that particular issue at this point. Retargeting mocap has been absolutely nasty onto those suckers, and thats the nastiness I'm facing with their bipeds on a daily basis at present. @General McMutton, inserting a forward facing gameobject in the character hieracy between neck and head nodes with a "SmoothLookAt" script is a low-fi alternative. Switch script on and off as needed to look where you want head to look. Not as fancy as full body though. ~A
Hi! I'm trying to get access to the size of the segments. I have to set it to "3". How can I get access to it and over elements to all parts of an element (first transform, last transform, etc.)? Thank you!
Hopefully someone else can explain why this works as well as it does, but I encountered a problem and a solution, so I thought I'd provide it. In some cases, the current implementation can result in a character's head turning not just in the horizontal plane, but also with a slight roll rotation. For bipeds this might not be terribly noticeable, and is likely the correct result, however with quadripeds, it gives them a very large desire to "cock their head" (like a dog), making them look ever-curious. This... is an interesting, but undesirable affect. I made the following alteration to fix it: // Look rotation in world space //Quaternion lookRot = ( // (parentRot * Quaternion.LookRotation(lookDir, segment.dirUp)) * // Quaternion.Inverse( parentRot * Quaternion.LookRotation( segment.referenceLookDir, segment.referenceUpDir ) ) //); // Look rotation in world space Quaternion lookRot = ( (parentRot * Quaternion.LookRotation(lookDir, parentRotInv * Vector3.up)) * Quaternion.Inverse( parentRot * Quaternion.LookRotation( segment.referenceLookDir, segment.referenceUpDir ) ) );
I was just about to complain about a necro-thread that was started in 2009 and idle since January, but anyone who posts Quaternion math and fixes a known problem gets a free pass in my book. Extra pluses for not resorting to Q->Euler conversions in your solution! Thanks.
hi! so I'm using your amazing scripts.. i managed to have my ragdoll head kind of following a target.. thing is i need to clone this ragdoll, and somehow when i clone it, the target is not working for the clones.. how can i solve this? thanks
The links seem to be dead, so is the link to the Unity procedural look controller. -Do any of these resources still work with Unity 2018 and 2019? Animations don't seem editable now, and the Animator seems to overide any changes you make to animated model, like turning a character's hear to look at the player while in an idle animation. Any advice on what to look for please?
I haven't looked at this for a while. The code is available at http://wiki.unity3d.com/index.php/HeadLookController but I haven't checked if it works in recent versions. The code doesn't rely on editing animations. It overrides poses on a per-frame bases in LateUpdate, which I believe still runs each frame *after* animation has updated poses.
Thanks for the info, I am still learning about Unity's animator system. I got that HeadLookController, and I will try to apply my animations AFTER Unity finishes with their's and see if that works. I still have reading to do, but I have collected a lot of info about "Inverse Kinematics" which people say is related to what I am trying to do. ??? -I appreciate your response, you are the best person to ask (being a Unity professional). Just to give some context so you know what I am doing, I have a rigged animated model character, they attack with eyebeams so he turns his head to look at the target. I have a script that rotates the head to look at the target. When being animated with the Controller, my "attack script" doesn't seem to move the head. When, I disable the animation controller, the script works fine. Like you said, I might have to apply animations after Unity does. Any advice helps.
Sorry for the necro!! I managed with animatePhysics by calling HeadLookController.Update after doing a Code (CSharp): anim.Update(0); // workaround, to make animator "catch up" with LateUpdate I hope that answer there justifies the necro bcoz i got my own problem... So i've been using this with a generic rig and it works great. It's a legless spirit, and the body-chest and neck-head segment can blend the LookAt nicely But then i recently added legs to the rig so it fits the Humanoid system, and ever since then, the neck-head part seems to rotate further than it should be, as if unaware of the body-chest's already rotating halfway On a pose where the chest is already looking straight, and .target is straight ahead On a pose where the chest is facing towards right, same .target position. Notice the head tilts waay to the left Anyone had this issue? Is there anything with Humanoid that makes this break, compared to Generic? Note i'm also not using any Animator.SetLookAtPosition whatsoever to even utilize Unity's Humanoid IK Cheers
Just wondering if anybody's tried this with hands, arms etc - pointing a gun at someone, for example?