- Home /
Spaceship gets stuck at transform.eulerAngles.x =270
I want my spaceship to loop the loop when i press up, to loop 360' , but it climbs to vertical and then it stays stuck upwards at transform.eulerAngles.x =270 and then when i press the buttons it just vibrates in stuck position.
if(Input.GetKey("w"))//------ NAVIGATION ----------
{
transform.eulerAngles.x += rotspd * Time.deltaTime * 7;
}
if(Input.GetKey("s"))
{
transform.eulerAngles.x += -rotspd * Time.deltaTime * 7;
}
i added this to prevent it becoming stuck at 270, but i still cant go more than 270', i.e. straight up.
if(transform.eulerAngles.x==270 )//------ stops it gettign stuck at 270degrees
{
transform.eulerAngles.x=275;
}
Answer by HappyMoo · Jan 21, 2014 at 01:35 PM
You can't use EulerAngles like that. Internally, Rotations are stored as Quaternions and there are more than one set of EulerAngles describing the same Quaternion, so what you put into EulerAngles might not stay the same.
A fast fix for your problem would be to keep your angle in a float and only change that float and only write to the eulerAngles, never read from them.
if(Input.GetKey("w"))
{
rotX += rotspd * Time.deltaTime * 7;
}
if(Input.GetKey("s"))
{
rotX += -rotspd * Time.deltaTime * 7;
}
transform.eulerAngles = new Vector3(rotX,0,0);
btw, what's doing the 7 there? This should be in rotspd. Multiplay you rotspd by 7.
The above should fix your problem. If you need to rotate around another axis, you may still experience gimbal lock, but that's for another time - maybe read up on "gimbal lock" first... and it may not apply to your situation
Thanks! i have learnt that there is gimbal lock in rotations. i was hoping there would be a kind of fix/framework in unity so that a programmer can put in x/y/z angles and an adaptive code translates it to quaternions.
sorry it's messy, it's the first code i wrote after reading tornado twins tutorial 2 years ago. here are the keys i was hoping for, it starts flipping insanely between +90 and -90 every frame, now i added z axis into New Vector3(rotx,roty,euler.z)
there other spaceship codes on unitycommunity.
if(Input.Get$$anonymous$$ey("w"))//------ NAVIGATION ----------
{
rotX += rotspd * Time.deltaTime ;
}
if(Input.Get$$anonymous$$ey("s"))
{
rotX -= rotspd * Time.deltaTime ;
}
if(Input.Get$$anonymous$$ey("a"))
{
rotY -= rotspd * Time.deltaTime ;
}
if(Input.Get$$anonymous$$ey("d"))
{
rotY += rotspd * Time.deltaTime ;
}
transform.eulerAngles = new Vector3(rotX,rotY,transform.eulerAngles.z);
//Roll craft:
if(Input.Get$$anonymous$$ey("r"))
{
constantForce.relativeTorque.z = 4;;
}
if(Input.Get$$anonymous$$ey("f"))
{
constantForce.relativeTorque.z = -4;;
}
try removing transform.eulerAngles.z. put a 0 and see how it reacts, Other than that, if you don't want any gimbal lock problems, you have to only operate on Quaternions and not on Euler Angles
Your answer
Follow this Question
Related Questions
Artificial Gravity "SpaceShip" 1 Answer
Flip over an object (smooth transition) 3 Answers
Turret. Detect if target is inside horizontal and vertical arcs 2 Answers
2D spaceship rotation 0 Answers