Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to make an object rotate up on the Y axis 90 degrees, then stop. I've got the rotating up bit working fine, it's getting it to stop once it hits 90.

Some of the things I've tried include the following:

float i = rotateSpeed * Time.deltaTime; 
while ( x != 90 )
        transform.Rotate( i, 0, 0);

        int x = 0; 

        if( x == 90 )
            transform.Rotate( 0, 0, 0 );

For some reason I can't get this simple thing to work. What am I missing / not doing?

share|improve this question

closed as too localized by Byte56, Jonathan Hobbs, michael.bartnett, Josh Petrie, Trevor Powell Oct 17 '12 at 5:01

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Is this code even working? How can you declare int x after the while loop, which actually iterates over x? Have you declared another x variable before? Please make sure that the pasted code is what you are actually running. –  Dan Oct 3 '12 at 16:20
Not to mention the infinite loop produced by the while statement. –  Byte56 Oct 3 '12 at 17:10

1 Answer 1

up vote 5 down vote accepted

You may want to study your basic control flow structures and learn about scope. Also, using a float to rotate and an int to count the number of times you rotated would only work if your i was exactly 1, which is very unlikely.

Depending on where this code is and how you want things to be displayed, you probably don't even want a while loop. A while loop would just run through all your rotation in one go, without updating whatever you're rotating on screen in-between. Basically having:

float totalRotation = 0; 
float rotationAmt = rotateSpeed * Time.deltaTime;
while(totalRotation < 90) 
    transform.Rotate(rotationAmt, 0, 0);
    totalRotation += rotationAmt; 

is the same (though less accurate) as :

transform.Rotate(90, 0, 0);

Likely what you really want, to have your object be updated on screen while it rotates is to store the objects current rotation somewhere and also store it's target rotation:

//stored in the object you're rotating
float rotation = 0; 
float targetRotation = 0;


//inside our update for the object check the rotation to see if we're where we want to be
float rotationAmt = rotateSpeed * Time.deltaTime;
if(rotation!= targetRotation) 
    //first we check to see if there's just a small amount of rotation to shave off
    // this check keeps us from never reaching the target rotation
    if(abs(rotation-targetRotation) < rotationAmt) {
        transform.Rotate(rotation-targetRotation, 0, 0);
        rotation += rotation-targetRotation;
    } else {
        transform.Rotate(rotationAmt, 0, 0);
        rotation += rotationAmt;

Now I know what you're going to do. You're going to copy and paste this code, and tell me it doesn't work. Don't do that. Just look at the code and see how it works. Then implement it yourself to fit your project. There are improvements that can be made to this code as well. Like checking which way to rotate to be fastest. Check this class to see how you might do that.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.