- Home /
Rotating an object to face the camera regardless of the cameras position.
Okay so far I have this script, this automatically faces the object towards the camera
var target : Camera; function Update() { var n = target.transform.position - transform.position; transform.rotation = Quaternion.LookRotation( n ); }
I want to know how to edit this script to give the object a 90 tilt to it always.
Cheers - C
Answer by VS48 · Oct 29, 2010 at 03:35 PM
On which axis should the 90-degree tilt be?
Update your code and change:
transform.rotation = Quaternion.LookRotation(n)
To:
transform.rotation = Quaternion.LookRotation(n) * Quaternion.Euler(0, 90, 0);
If that's not the right direction, just change the parameters of Euler() depending on your axis and direction of rotation; maybe Euler(90,0,0) or Euler(0,-90,0), etc.
Edit: Slowly change direction
var newRotation = Quaternion.LookRotation(n) * Quaternion.Euler(0, 90, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 1.0);
Not sure if that's what you're asking for, but hope it helps... Change 1.0 to control easing speed.
(Note, since it's interpolating from the current rotation, it is not linear, though that's often not desired anyway)
Thanks a lot, Got another quick question, after a curtain point can i get my plane to flip around, so its always facing the right direction.
No prob! Sorry -- not sure what plane you mean -- see my edit if you mean you want to change the direction slowly over time.
Thats useful, but what i mean is I have a sphere with a small "plane" arrow pointing down to it, the camera orbits the sphere all around but after a certain point the arrow disappears "inside" the sphere.
Answer by skovacs1 · Oct 29, 2010 at 04:23 PM
If you wanted to face the camera, wouldn't it just be easier to do something like:
var cam : Camera;
function Update() { transform.LookAt(cam.transform); }
And for your tilt, your object's axes are probably misaligned.To maintain the orientation of your misaligned axes, you could do something like:
var cam : Camera;
function Update() { transform.LookAt(cam.transform, transform.up); }
or with your posted code, merely change your LookRotation to this:
Quaternion.LookRotation(n, transform.up);
You really should however fix your misaligned axes or they will likely cause you more problems in the future.
Answer by leith · Oct 29, 2010 at 05:16 PM
I get the impression what is meant is that the "roll" of the object is 90 degrees off. In which case, using a "look at" along with an up vector from the camera is probably what is needed.
something like:
obj.transform.LookAt(cam.transform.position,cam.transform.up);
And if that "up" is the wrong up, change the up vector to the camera's left or right, etc.
obj.transform.LookAt(cam.transform.position,cam.transform.left);
the above is pseudo-code. untested.