- Home /
rotate camera to face a specific object?
Hi,
I would like to move my camera at a specific moment, and make it look at a plane with an animated texture. So I would like to copy the rotation of the plane (that is outside of the landscape), and move the camera a little bit backwards so that it can look correctly at the plane.
But I can't make it work: the first chunk of code does not rotate properly and the angle is not good, and the second chunk of code does not look at all at the plane ... Would you know how to do this?
Here is the code :
void Update () {
if (moveCamera){
/*transform.position = toFollow.position;
transform.eulerAngles = toFollow.eulerAngles;
transform.position += Vector3.up * 8;
transform.LookAt(toFollow.position);
*/
animation.Stop();
Vector3 relativePos = toFollow.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
transform.position = toFollow.up * 8;
}
}
EDIT :
Here is the image with :
transform.forward = -toFollow.up;
(and the angle on the last image on the right) :
125125
If I play with Quaternion, I only get a result with
transform.rotation *= Quaternion.FromToRotation(transform.up, -toFollow.forward);
, but in this case it gives me exactly the same as the image above. ?
Thanks
Answer by whydoidoit · Mar 19, 2013 at 01:44 PM
Yeah planes are a pain because they kind face in the wrong direction!
This might well work for you:
transform.position = toFollow.position - toFollow.up * 8;
transform.forward = -toFollow.up;
or
transform.rotation = Quaternion.LookRotation(-toFollow.up, Vector3.up); //Not sure what your up direction should be here
Thank you very much! It works fine, with "+ toFollow.up"! The second option works also, but the camera is not exactly at the right rotation with both ways, it is a bit rotated to the right ? I have tried to copy the euler coordinates but Unity does not allow me to do it, would you a way to achieve this? The camera faces in the right direction, it is parallel to the plane, but with a slight angle to the right...
Right that makes sense - neither of those is caring about the rotation about the axis facing the plane - how about this:
transform.forward = toFollow.up;
transform.rotation *= Quaternion.FromToRotation(transform.up, toFollow.forward);
That might be toFollow.right (ins$$anonymous$$d of forward) or -that or -.right!!!
@whydoidoit : thanks a lot, so I edited my first post with an image, I am not sure which values to rotate, -toFollow.forward faces the plane but with the same angle as before! I might be doing something wrong, so I posted the image above! Would you have any idea? Thanks again!
Ah to hell with it :) It's one of those combinations of right or forward!
Let's try this:
transform.forward = toFollow.up;
var angles = transform.localEulerAngles;
angles.z = 0;
transform.localEulerAngles = angles;
Alright! It finally works! I changed z in ".y = 0" and I changed the plane's rotation, with "0, 180, 0"! Thanks a lot for your help!