- Home /
How can i perform any action after some time interval
Hi Everyone, Suppose i have a TWO regions A and B, and i want to perform some action when my object will be stay more then 3 seconds in region B. previously my object is in A region.
When i am using Time property then time Counting is start when my scene is load, or if i am writing it in any function then on scene load my Time counting is start.
Like time.deltaTime; So how can i Count time and after reset it to ZERO.
@VIPINSIRWANI you must TIC$$anonymous$$ an answer, thanks - it's the only way to get karma points so you can ask more quetsions unmoderated.
Answer by Joyrider · Aug 12, 2013 at 10:18 AM
You can either launch a coroutine that checks that your user is still in the region, and after 3 seconds, performs some actions. If he leaves, it ends the coroutine. (that would be my choice)
IEnumerator doActionIfStayInZone(int zoneID, float timeDelay)
{
float elapsedTime = 0;
while(zoneID == currentZoneID) // with currentZoneID being a global variable
{
if(elapsedTime>timeDelay)
{
// do your action here or launch new action coroutine
break;
}
elapsedTime = Time.deltaTime;
yield return null // this means wait 1 frame, you could reduce the number of checks by cheking up every second or 0.5 s with yield return new WaitForSeconds(0.5f)
}
yield return null
}
And you can launch the coroutine a number of ways, through triggers, position detection,... it depends on your scene and your objectives.
or have 3 variables:
one indicating the region you are in
another setting the "entryTime".
and a third "actionDone" indicating your action was performed
Every time you change zone, the entryTime should be reset to Time.time, and the third variable actionDone reset to false;
currentRegion = "nameOfTheCurrentRegion"; // could also be an object, a IDnumber,...
entryTime = Time.time;
actionDone = false;
Now you can check up on (Time.time - entryTime > 3 sec) and on the region you are in. if it is the region you want to do something in, the difference is bigger than 3 sec and (actionDone == false), set actionDone to true and perform your action.
if((Time.time - entryTime > 3) && (actionDone == false))
{
actionDone = true;
// Do your action
}
@Joy, it is staggeringly easier to just use Invoke().
In particular, beginners should master Invoke() before worrying, at all, about advanced concepts like yielding, etc.
unityGE$$anonymous$$S.com for many good articles on this
@Fattie, if it were just about doing some action after some time once you set foot into a region, I'd agree. But here you have to check if the user is still there after 3 seconds, AND never left. I don't really see how invoke is going to get us there?
Hey Joy!
one incredibly simple algorithm for this is ..
(A) the user arrives in the box. do this:
Invoke( "success", 3.0 );
(B) the user leaves the box. do this:
CancelInvoke( "success" );
(C) write a function called "success()". when success() is called ......... it is a fact that the user has been there for three seconds and never left
This in-out pattern is so common in vid games that you sometimes just build it in to triggers. (ie, "every single time" you put up a "trigger wall" that a character walks through, the one and only reason you do it is for the algo I describe here - so you just bolt it on to triggers, as if that is their only reason for existence, heh!)
BTW this is an excellent example ("the" example?) of why CancelInvoke is so great.
Hehe, okay, nice, never noticed the CancelInvoke there ;) Haven't really been an Invoke user until I discovered it recently (well after understanding coroutines).
Anyway, good to know ;)
Answer by Fattie · Aug 12, 2013 at 01:46 PM
Hey @vipin, I just actually read your question (thanks to Joyrider :) )
the algorithm for this is incredibly simple ..
When you enter region B, always call: Invoke( "success", 3.0 );
When you enter region A, always call: CancelInvoke( "success" );
then write this routine
function success()
{ Debug.Log("you were, in fact, in B for 3 seconds"); }
it's that simple. Conceptually the fact that you can name the timer makes all the difference (it's exactly why Unity did this).
Hope it helps. if you are a new user be sure to TICK any answer to close out the question, thanks.
Your answer
Follow this Question
Related Questions
Flashlight Cooldown and Timer 1 Answer
How does DeltaTime handle game logic ? 1 Answer
Why when changing object position it's taking few seconds until changing the position ? 0 Answers
Lowering Time.timeScale and lowering Time.fixedDeltaTime 1 Answer
Can anyone tell how can i add 2 seconds to my TIMER from another script ? 2 Answers