How to make a bone look at something?

I know about the LookAt function but does anyone know how I can get the head to look at a target and move the arm bones up so I can like point the guns at the target?

there is a character head lookat package available on the unity resources site.

what I do is make a script with variables to select the joints of the character I want to control (e.g. “neck” joint).

my script then:
–creates an empty game object (go) and rotates it to align with the character’s forward vector
–sets the go the position of the joint
–finds the parent joint of the joint
–sets the go as a child of the parent joint
–sets the joint as child of the go

then the script uses LookAt (or a Quaternion.Lerp) to aim the forward vector of the go at the target. This was done because the rotation of the joint is likely to be arbitrary to what is the correct forward “look” of the character. And of course when the scene is stopped, the inserted go is removed and so it is non-destructive to the character joint hierarchy.

A similar thing can be done with shoulders and elbows but more difficult to get it right.

1 Like

Sounds like a bit much work to get a look rotation. How about something along the lines of this?: (untested)

// Given: Transform headTransform, Transform target;
void LateUpdate ()
{
    Quaternion lookRotation = Quaternion.LookRotation (target.position - headTransform.position);
    headTransform.rotation *= lookRotation;
}
1 Like

good point -
I guess I am always thinking hierarchies and being able to visualize the rotations clearly.

in my method, I have also been using raycasts from the forward vector of the control object, and also using local space with InverseTransformPoint to know what is in front or behind the direction he is looking. These all just seemed easier to have a control object that has a uniform alignment, but I agree with your simpler method for the rotation.

Im sorry i dont really understand what you are trying to say here. Do you want me to make an empty game object, set the rotation same as the head and make the head or the neck child of the emty game object and attach the LookAt script to the empty game object?

Im still a newbie, only started learning unity a month ago and dont have much experiance in programing so I dont really know what the above script means and im also getting errors when I put it into unity. Can you explain what the above is doing and what should I connect this to. Thanks

What does your current script look like?

i dont have one because i dont know where to start =/ as i have said before i only started using unity a month ago.

AngryAnt may have asked that because you said you were getting errors when you put it into Unity.

His code example is complete except does not include the actual place where “target” and “headTransform” have to be defined as variables of the type Transform.

Check out these as starters for scripting:
http://download.unity3d.com/support/Tutorials/2%20-%20Scripting%20Tutorial.pdf
http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)

I would suggest you do some more scripting tutorials - like the ones priceap linked to - before diving into scripting heavy projects.

If you did a direct copy paste of my example into a script file without modifications, that would seem to indicate that you need a bit better understanding of programming.

Hi AngryAnt -
I tried out that example and it does not seem to work right.

Doing headTransform.rotation *= lookRotation makes the headTransform spin crazy.

Maybe it should be something like this?:

var headTransform : Transform;
var target : Transform;
private var initialRotation : Quaternion;

function Start () {
	initialRotation = headTransform.rotation;
}

function LateUpdate () {
	var lookRotation : Quaternion = Quaternion.LookRotation (target.position - headTransform.position);
	headTransform.rotation = lookRotation * initialRotation ;
}

Yea sorry - didn’t really provide enough info there. The reason for applying the relative rotation every frame and doing it in late update is to apply it to an animated character. The bit of the frame layout interesting for this case is:

Update
animation is applied
LateUpdate
frame is rendered

This means that every frame, whatever changes you made to bones in the previous frame gets reset by the animation. In LateUpdate you can then modify the end transformation for the final frame.

The result is that you don’t sacrifice animation of the head just because you need to do head look.

2 Likes

I know it’s(many) years later now, but thanks a lot!

1 Like

@scarletsnake helped me a lot too. LateUpdate was crucial information.