Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by VIPINSIRWANI · Aug 12, 2013 at 09:57 AM · timetime.deltatimetimeout

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Oct 15, 2013 at 10:52 AM 0
Share

@VIPINSIRWANI you must TIC$$anonymous$$ an answer, thanks

avatar image Fattie · Oct 15, 2013 at 10:52 AM 0
Share

@VIPINSIRWANI you must TIC$$anonymous$$ an answer, thanks - it's the only way to get karma points so you can ask more quetsions unmoderated.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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
 }


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Aug 12, 2013 at 12:56 PM 0
Share

@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

avatar image Joyrider · Aug 12, 2013 at 01:15 PM 0
Share

@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?

avatar image Fattie · Aug 12, 2013 at 01:42 PM 3
Share

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.

avatar image Joyrider · Aug 12, 2013 at 01:46 PM 0
Share

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 ;)

avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges