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:
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.
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: