- Home /
 
               Question by 
               Dave 11 · May 23, 2011 at 05:53 PM · 
                rotationrotatefunctionloopangleeuleranglescustomfunctionscallrotatingwhileloops  
              
 
              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.
               Comment
              
 
               
              Answer by diabloroxx · May 23, 2011 at 06:22 PM
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;
 }
Your answer
 
 
             Follow this Question
Related Questions
Always rotating an object to a set angle? 0 Answers
Is 360° the same as 0°? 2 Answers
Delayed rotation when i use transform.rotate 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                