- Home /
Rotating an object with the camera?
I'm trying to keep an object (many objects really) within the camera's view. The problem is that Quaternion.Angle only returns positive values. So what I have only works when the camera looks to the right. If it looks to the left, all the objects just rotate even further to the right. I've attached the following script to each object that I want to remain in view of the camera. Globals.lastRot is the mainCam's rotation the previous frame and tarPos is the position slightly in front of the main camera.
Any suggestions for a way to tell when to negate the angle? I saw some other post saying to use cross product, but that's not working for me.
void Update()
{
float dAngle = Quaternion.Angle(Globals.lastRot, Globals.mainCam.transform.rotation);
//floats suck
if(dAngle < 1)
{
dAngle = 0;
}
if(dAngle != 0)
{
//Check if rotated left?
if(Vector3.Cross(Globals.lastRot.eulerAngles, Globals.mainCam.transform.rotation.eulerAngles).y < 0)
{
dAngle = -dAngle;
}
}
gameObject.transform.RotateAround(tarPos, Vector3.up, dAngle);
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How to move camera up and down 1 Answer
maximum angle for camera 0 Answers
Moving Camera on player stop then move and rotate to separate point? 0 Answers