I have a robot that is running any angle around a spherical planet and I want to rotate his head so that it looks where the camera is pointing, and I have managed to with transform.Rotate, except that the head flips and vibrates every frame because it is an additive rotation rather than setting a new one.
This line makes the correct rotation for some reason:
head.transform.Rotate(-camdir.transform.localEulerAngles.y*2,90,-camdir.transform.localEulerAngles.x*2);
multiplying by 2 means that the head turn more and it looks good.
How do I set the same angle to a new rotation instead of using additive Rotate?
So the rotation is fine, but off by 90 degrees? Assuming I’m visualizing the geometry of your game correctly, this means the head is authored wrong. That is when you bring the head in from the 3D modeling program, the face should be looking at positive ‘Z’ when the rotation is (0,0,0). If you cannot fix this in the modeling program, you can compensate by using an empty game object. Place the empty game object so the body, then make the head a child object with its rotation to make it look forward. The script goes on the empty game object.
You may be able to fix it by “hacking” around with the rotation code. For example you could try:
head.transform.rotation = Quaternion.LookRotation(camdir.transform.right);
or if that is 180 degress out of line:
head.transform.rotation = Quaternion.LookRotation(-camdir.transform.right);