Anti-Gravity vehicle bounce-back help.

Hello, i am making a game where you control a ufo like Anti-Gravity-vehicle, the problem is, when the saucer hits something and it tilts i want it to smoothly bounce back to its original rotation, ive made a script but there is a problem.

var xRotation:float;
var xForce:float;
function Start () {

}

function Update () {
xRotation=transform.localEulerAngles.x;
	if(xRotation==270)
	{
		xForce=0;
	}
	if(xRotation>270)
	{
		xForce=-50;
		rigidbody.AddRelativeTorque(xForce,0,0);
	}
	if(xRotation<270)
	{
		xForce=50;
		rigidbody.AddRelativeTorque(xForce,0,0);
	}
}

im testing it on the x axis for. the script relies on transform.localEulerAngle.x/z for rotation detection. but for some odd reason when the craft tilts the positive x the value in the transform.eulerAngles (the one in the inspector) goes up (like normal) but when it tilts negative x the value still goes up, resulting the craft rotating to 0 instead of 270 where its supposed to be, making the crafts front facing up!, what!??
Thanks in advance, really appreciate it.

Could this be the problem:

add a line Debug.Log(" the angle is now … " +localEulerAngles.x);

you’ll see that the angle is expressed:

#from 0 to 360 degrees
.

in other words, “355” means “5 left” and “13” means “13 right”

This is always an ongoing huge problem in video games. There are different ways of expressing angles, and you’re always needing to change between them!!

In this case, you probably want it more like this:

#“-5” means 5 left, and “+13” means 13 right
.

If that makes sense, all you need to know is how to convert from “0 to 360 style” to “plus and minus style”.

Fortunately this is easy to do.

It’s basically: if the angle AA is bigger than 180, then AA = ( AA - 360.0 );

So for example if AA is 358, you get 358-360 = minus two. So that’s correct.

So FIRST you have to convert to “plus and minus style”

THEN you have to totally delete all your complicated code above regarding 270, etc

THEN you have to write new code, based on the plus or minus style" - heh - which is really easy.

Lemme know if this fixes the woe !