Caiuse
October 29, 2010, 3:01pm
1
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
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)
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.
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.