- Home /
How to make Quaternion a negative
Hi All!
I got this script hanging on a Cube skybox, wich is a child of the main camera. It has to rotate horizontally against the rotation of the camera.
How do I make a variable that is the negative of the Camera's rotation? This is the script I got so far.
function FixedUpdate()
{
var rot1 = Camera.main.transform.rotation;
print(rot1);
transform.rotation = rot1;
}
So, in short: How do I make var rot1 a negative? It returns values like this: (0.0, 1.0, 0.0, 0.1)
Thanks in advance!!
transform.rotation is a quaternion (that's why you're getting a return of 4 values). Can you explain a little more clearly exactly what you're trying to do?
The script is attached to a cube skybox, the mobile skybox in the standard assets. The skybox is a child of the main camera, wich means it moves along good, it keeps the camera centered in the cube. But when the camera rotates, the cube should rotate the other way, otherwise the sky doesn't seem to move. So the rotation value for the cube should be the inverse / opposite / negative value of the camera's rotation. How do I do this..
Can't believe by the way that this isn't part of that standard asset. As it is now, it's only a cube with a shader and a texture :D
Answer by Bunny83 · Aug 06, 2011 at 11:05 AM
Ehm actually Quaternion.Inverse() will invert the rotation so you need to set the localRotation to the inverse of the camera. But it's much simpler to set the .rotation to Quaternion.identity.
.rotation is the rotation in world-space. Setting it to identity will keep the skybox-rotation always world aligned.
Also don't use FixedUpdate() it's the physic-loop and has nothing to do with the visual result. LateUpdate is the place to go:
function LateUpdate()
{
transform.rotation = Quaternion.identity;
}
Another way is to keep the skybox as a seperate object and just move it along with the camera. That way the rotation won't change ;)
var camera : Transform;
function LateUpdate()
{
transform.position = camera.position;
}
Answer by DaveA · Aug 06, 2011 at 12:28 AM
In theory you would negate the 4th element. But maybe you need to convert it to Axis-Angle first, then negate the angle, then reconvert back to Quaternion. All those api's exist.
Sounds like an interesting skybox. How is it different from a normal one, like you set in the Rendering settings?
It's main difference is that it uses only one texture and only costs 6 drawcalls. So excellent for $$anonymous$$obile devices..
I'm a bit of a n00b when it comes to Javascript, could you perhaps give me an example of what you mean? $$anonymous$$y expierence is only with easy languages, so making things a negative hasn't been that hard in the past:)
Ow, and I should point out, that since it is for a mobile project, it has to be as fast / low cpu cost as possible.