Roation doesn't stop

So I have a landing gear for an aircraft which is supposed to go up stay up when I press “G” the gear does go up when I press G but the problem is, it doesn’t stop rotating when it reaches the angel I want it to stop at.

function Update()
{
if(Input.GetKey(“g”)){
Gear = true;
}

if(Gear == true)
{
leftWheel.transform.Rotate(0,0,1);
}

if((Gear==true) (leftWheel.transform.eulerAngles.z == 211))
{
Gear = false;
leftWheel.transform.Rotate(0,0,0);
}
}

The problem is that the angle of rotation wont ever (or very unlikely) be exactly 211

Do a range check instead e.g.

z >= 211 z < 360

Thanks a lot mate that worked! But weird 'cause I had to do like z>90 z<215 to get it to stop at 208 :slight_smile:

Thanks loads again!

Actually scratch that, it doesn’t really work properly 'cause everytime I run it it stops at a different angle that angle mostly is not what I need it to stop at

Couple of things:

Use a smaller range
Snap the rotation to the correct rotation if it’s within range
Use an animation instead

Factor in Time.deltaTime too, so the gear moves at the same speed regardless of frame rate.

There’s no point in rotating zero degrees. Don’t rotate at all.

You can consolidate your code a lot too. Something like this.

function Update(){
    if(!gearUp  Input.GetKey("g")){
        leftWheel.transform.Rotate(0,0, gearSpeed * Time.deltaTime);
        gearUp = leftWheel.transform.eulerAngles.z >= gearUpAngle;
    }
}

If you want to be absolutely sure that the gear doesn’t overshoot - because they gear’s visible or something - you can use this.

function Update(){
    if(!gearUp  Input.GetKey("g")){
        leftWheel.transform.Rotate(0,0, gearSpeed * Time.deltaTime);
        if(leftWheel.transform.eulerAngles.z >= gearUpAngle){
            left.Wheel.transform.eularAngles = Vector3(0, 0, gearUpAngle);
            gearUp = true;
        }
    }
}

Thank you very much for all the replies fellas.

Japser, that actually fixed it! Thank you very much again it’s perfect now :slight_smile: