- Home /
 
Creating function that lasts for x seconds
Hey guys,
Not sure if I'm just stupid right now but I got a small problem. I want to create a function that lasts for x seconds.
In my case I want to call a function that rotates an object by x degrees. But I don't want the object to be rotated instantly (within 1 frame) but slowly. The process of the rotation should always last for 1 second. So this is what my script looks like:
 function Update ()
     {
     if(Input.GetKeyDown("r"))
         {
         RotateObject(90);
         }
     }
     
 function RotateObject (degrees : int)
     {
     transform.RotateAround(Vector3.zero, Vector3(0,0,1), degrees * Time.deltaTime);
     }
 
               Has anybody an idea how I can make the function RotateObject last for 1 second?
Answer by wesleywh · Apr 14, 2015 at 07:55 PM
Hum... Well I am not sure about the exactly one second timing here but this can give you a good example of how I might do it. You can look for better ways of timing it if you would like:
 var timeCounter:float = 0.0f;
 var timeToRotate:float = 1.0f;
 function Update ()
      {
      if(Input.GetKeyDown("r"))
          {
             if(timeCounter < timeToRotate){ //Added this to your code to execute this script only if it is under 1 second time count.
                 RotateObject(90);
                 timeCounter += Time.deltaTime;
              }
          }
      }
      
  function RotateObject (degrees : int)
      {
        
      transform.RotateAround(Vector3.zero, Vector3(0,0,1), degrees * Time.deltaTime);
      }
 
               You of course would have to decide how you would want to reset the variable timeCounter back to zero to count again if you wanted to.
Answer by elijahpeckman · Apr 16, 2015 at 06:42 PM
I'd use a Coroutine. Coroutines are specifically designed to allow you to stop what you're doing in the middle of a frame and come back to it next frame. I'm really only familiar with coding in C# at the moment so I don't trust myself to write an example code, but I encourage you to check the documentation and tutorial:
http://docs.unity3d.com/Manual/Coroutines.html http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
Answer by YoungDeveloper · Apr 16, 2015 at 09:06 PM
For non constant time events you should use coroutines. This should start you going. This will rotate the mesh infinitely.
 private var isRotating:boolean = false;
 
 function Update (){
     if(Input.GetKeyDown("r")){
         if(!isRotating){
             isRotating = true;
             RotateObject();
         }
     }
 }
 
 
 function RotateObject() {
     while(true){
         transform.RotateAround(Vector3.zero, Vector3(0,0,1), 90 * Time.deltaTime);
         yield;
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
LookAt becoming inaccurate over time 0 Answers
Time Function in Javascript? 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers
Rotate object on the Y axis 90 degrees every 5 minutes? 1 Answer