- Home /
Store initial rotation in JavaScript
So what I'm trying to figure out, and could not decipher from other answers found here, is exactly how to store the rotation of my ship so that I may use it to reset to zero later on in the script. I thought I was doing it correctly, but obviously I'm missing something very important here. Could someone please help me out and explain what I'm doing wrong? Again, this is Javascript.
var originalRotationZ = Quaternion;
function Start ()
{
transform.rotation.eulerAngles.z = originalRotationZ; // store rotation on start
}
Answer by Eric5h5 · Oct 28, 2012 at 05:22 AM
This is wrong:
var originalRotationZ = Quaternion;
Quaternion is a type, not a value that you can assign to variables. It's unclear if you're trying to store the entire rotation or just the Z axis. So it's either:
var originalRotationZ : float;
function Start () {
originalRotationZ = transform.eulerAngles.z;
}
or
var originalRotation : Quaternion;
function Start () {
originalRotation = transform.rotation;
}
This seems like what I'm looking for. I'm just trying to store the Z rotation so that when I release the horizontal axis it returns slowly to the original position. I'll give this a try when I have energy to do it in the morning and let you know how it turned out.
Answer by Muuskii · Oct 28, 2012 at 04:09 AM
You are trying to store a Quaternion in eulerAngles you can instead; store the eulerAngles in a Vector3 and then set them as shown in the first link. Or you can do this:
transform.rotation.eulerAngles.z = originalRotationZ.eulerAngles.z;
which according to Transform-eulerAngles
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.
Your answer
Follow this Question
Related Questions
Saving Runtime Changes (JS) 1 Answer
Damping Rotation Reset 1 Answer
transform.rotation.z gives different value than expected 1 Answer
Set rotation values equal to 0 5 Answers
Why does my rotation lock? 2 Answers