Hello unity3D.I have a question about timers?What is the easiest way to create a timer in javascript?For example,my characters does this kind of move that makes the camera move to another position and i need a timer because i want the camera to go back to the starting point whenever there is no collision happening more than 2 secs.if anyone knows how i can do this?Can you please tell me how?
Untested, just set timerActive to true to start timer again.
var time : float;
var timer : float;
var timerActive : bool;
void Start()
{
timerActive = true;
time = 2.0;
}
void Update()
{
timer += Time.deltaTime;
if(timer >= time && timerActive)
{
//Move Camera
timerActive = false;
timer = 0.0;
}
}
You could use a coroutine like so :
IEnumerator DoAfterTime(float time){
yield return new WaitForSeconds(time);
// do something after time
}
void StartTimer(){
StartCoroutine(DoAfterTime(2f));
}
I guess this could be improved further by passing a callback function to the coroutine and calling it in place of “do something”