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
0
Question by Baalhug · Dec 01, 2013 at 06:27 PM · floateventdeltatimeonce

How can i manage an instant event with float type

Hi, guys. I have a tricky problem when i try to manage an event ocurring when a variable float reaches a certain value. I use:

varfloat+=Time.deltaTime;
if (varfloat>= instant) RunEvent();

This works fine, but what happens when i only want this event to trigger once? First i thought to calculate every tick from Time.deltaTime but it happens Time.deltaTime is not constant so this won't work:

if (varfloat >= instant && varfloat < instant+Time.deltaTime) RunEvent();

Because i dont know how long the next frame will be (it's not constant). I also tried Mathf.Approximately but the approximation is smaller than frame times so it does not work either. It is important the event to be called ONCE.

I hope you understand my problem and you had to deal with this before :_(

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Owen-Reynolds · Dec 01, 2013 at 06:57 PM

General timer comment: Unity already runs a clock for you, so it's easier not to add time.deltaTime yourself. Imagine you want to wait 5 minutes for real. It's 2:43 now, so you write down 2:48. Then you just keep glancing at the clock until 2:48.

Non-coroutine solutions might be 1) when you fire the event, reset the time to make sure it can't go off again:

 // setting a 2 second delay:
 timerVar = Time.time + 2.0f;

 // waiting 2 seconds:
 if(timerVar > Time.time) {
   RunEvent();
   timerVar=9999999;
 }

Or, 2) use an extra "I'm waiting to fire" variable.

 bool waitingForEvent1=false;

 // setting a 2-second delay:
 timerVar = Time.time + 2.0f;
 waitingForEvent1=true;

 // waiting 2 seconds:
 if(waitingForEvent1 && timerVar>Time.time) {
   RunEvent();
   waitingForEvent1=false;
 }
Comment
Add comment · Show 3 · 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 Baalhug · Dec 01, 2013 at 08:01 PM 0
Share

Your comments are nice, Owen, but i wrote down the time example to simplify the problem. Actually varfloat is the position of a car on a path, and it is calculated from speed and player decissions so i will never know the delay before the car is at the desired point (instant). Anyway, the solutions you propose in your answer are fully functional but i cant use option 1) because i need the timer (the position of the car) keep growing along the path (or car will stop), and option 2) is nice but i will need lots of "flag variables" because there are a lot of points in the track (positions on the path) that trigger events.

avatar image Owen-Reynolds · Dec 01, 2013 at 11:29 PM 0
Share

An old trick to never repeat something during "advancement" is to have an int variable like "lastPlaceTrigered." If you're past waypoint6, do the 1-time thing if lastPlaceTriggered is less than 6. Then change LPT to 6.

avatar image Baalhug · Dec 01, 2013 at 11:45 PM 0
Share

That's a really elegant solution to avoid declaring many "flag variables". But after reading your comment about coroutines they seem very interesting, i hadn't use them before, so i will take a look at their behaviour even if finally i use the int variable solution. Thank you very much, Owen. Turn your comment into an answer so i can accept it.

avatar image
0

Answer by Mortoc · Dec 01, 2013 at 06:37 PM

Sounds like a decent time to use a Coroutine. Something like this:

 IEnumerator RunEvent()
 {
     while(varfloat < instant) 
     {
         varfloat += Time.deltaTime;
         yield return 0;
     }
     RunEvent();
 }
Comment
Add comment · Show 3 · 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 Owen-Reynolds · Dec 01, 2013 at 07:04 PM 0
Share

Could replace the while loop with 1 line like:

yield return new WaitForSeconds(timeDelay);

There's also a short-cut coroutine if you only want to add a delay on a 0-input function: Invoke("RunEvent", delay);

...and, rename to not have 2 RunEvents.

avatar image Baalhug · Dec 01, 2013 at 07:54 PM 0
Share

Well i think $$anonymous$$ortoc's method won't work for me because i need varfloat out of the coroutine for further calculations. Still i dont understand how that function works. When varfloat < instant it increases varfloat and returns waiting for another call. When varfloat is equal or greater than instant then it calls itself again and enters in a void loop.

avatar image Owen-Reynolds · Dec 01, 2013 at 11:34 PM 0
Share

Coroutines are special things. The yield return 0; means: pause right here until next frame. It's like a sleep() command, and unlike any normal program$$anonymous$$g command. I think a coroutine would work, but your particular problem might not be the best way to learn them.

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

18 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 avatar image

Related Questions

ERROR: It is not possible to invoke an expression of type 'float'. 1 Answer

Calling a method once in update 1 Answer

Float and deltaTime problems? 1 Answer

How to change this using a coroutine 1 Answer

Add a dynamic float function to an onValueChanged Event (Slider) 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