x


How do I call a function containing a while loop, inside the update function?

Ok, so I'm trying to rotate my object, while a certain variable is not equal to another variable, this is the script I got.

private var go : boolean = true;
private var angle : float;
private var currAngleY : float;

function Update () {

if(Input.GetKeyDown("a")) {
       rotate("left");
    }
}

function rotate(direction : String) {
    Debug.Log("rotating");
    if(direction == "left") {
       if(go) {
         currAngleY = transform.eulerAngles.y - 90;
         go = false;
       }
       while(currAngleY <= transform.eulerAngles.y) {
         angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, currAngleY, 20 * Time.deltaTime);
         transform.eulerAngles = Vector3(0, angle, 0);
       }

    }

}

As soon as I try to run this, Unity just freezes and I have to hardreset it. Is there a way to make my object turn smoothly for only 90 degrees each time and then make it stop until the next input is given.

more ▼

asked May 23 '11 at 05:53 PM

Dave 11 gravatar image

Dave 11
133 8 9 20

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Update runs once every frame, so it's clearly not suitable for that sort of thing. See here.

more ▼

answered May 23 '11 at 06:07 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Thanks, this does what I want it to do. But it still needs some tweaking :)

May 23 '11 at 06:34 PM Dave 11
(comments are locked)
10|3000 characters needed characters left

Perhaps you can use If condition along with various flags to achieve what you want. Since update already does what you want your While loop to do.

For example,

if(conditionFlag)
{
//Your Rotate function
conditionFlag = false;
}

if(inputGiven)
{
conditionFlag = true;
}
more ▼

answered May 23 '11 at 06:22 PM

diabloroxx gravatar image

diabloroxx
689 5 7 12

I'll try this one too. Thanks for the reply.

May 23 '11 at 06:34 PM Dave 11
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2169
x725
x478
x295
x287
x199
x135
x82
x76
x60
x55
x22

asked: May 23 '11 at 05:53 PM

Seen: 1240 times

Last Updated: May 23 '11 at 06:34 PM