Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
9
Question by DubstepDragon · Feb 25, 2013 at 06:35 PM · timerweaponcooldown

Usage timer/weapon cooldown...

What is a possible way of having a cool-down timer for a weapon?

Please consider that all my work is in C#.

Thanks in advance!

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

3 Replies

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

Answer by robertbu · Feb 25, 2013 at 07:15 PM

One easy way is to use a what I call a time stamp. Anytime you want a cool down you would do something like:

 timeStamp = Time.time + coolDownPeriodInSeconds;

The in your firing code, you would only allow the gun to fire if the timeStamp

 if (timeStamp <= Time.time)
 {
 // Your firing code
 }
Comment
Add comment · Show 8 · 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 DubstepDragon · Mar 02, 2013 at 05:25 PM 0
Share

Amazing, dude! Thanks! I like your Time Stamp theory, might consider referring it to my friends too. You are awesome!

avatar image moynzy · Oct 22, 2014 at 12:54 AM 1
Share

@robertbu what do timeStamp and cooldown perdiods in second stand for?

avatar image robertbu · Oct 22, 2014 at 01:17 AM 1
Share

'timeStamp' is a float declared at the class level. In Javascript it would be:

  private var timeStamp : float;

...in c# it would be:

  private float timeStamp;

'coolDownPeriodInSeconds' is the number of seconds for the cooldown. You can replace it with a constant like 2.5, or you can declare it as a variable, and assign it a value.

avatar image shieldgenerator7 · Jan 17, 2016 at 09:21 PM 0
Share

Nice and elegant solution! Thanks!

avatar image immafirinmahlazor · Mar 11, 2017 at 02:42 PM 0
Share

WHERE DO WE PLACE THE$$anonymous$$ ON THE SCRIPT?

avatar image usr_pyth immafirinmahlazor · Feb 25, 2020 at 07:23 PM 0
Share

Initialize the variables at the top of the script, then when you want to start the timer, do timeStamp = Time.time + coolDownPeriodInSeconds;. When you're getting the input from the player, only do the ability if(Time.time <= timeStamp)

Show more comments
avatar image
1

Answer by tbriz · Jan 09 at 08:23 PM

In my opion, this is way better than timestamps.

Simple example using a proper coroutine.

     public bool IsAvailable = true;
     public float CooldownDuration = 1.0f;
     
     void UseAbility()
     {
         // if not available to use (still cooling down) just exit
         if (IsAvailable == false)
         {
             return;
         }
         
         // made it here then ability is available to use...
         // UseAbilityCode goes here
         
         // start the cooldown timer
         StartCoroutine(StartCooldown());
     }
     
     public IEnumerator StartCooldown()
     {
         IsAvailable = false;

         yield return new WaitForSeconds(CooldownDuration);

         IsAvailable = true;
     }
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
avatar image
-1

Answer by Mizacoto · Jul 28, 2020 at 07:25 AM

You can also use Coroutines in unity. They aren't that hard to use and save you a lot of time in the future if you plan to have multiple cool downs going on. Here's an Example:

 void Update()
 {
      StartCoroutine(TimerRoutine());
 }
 
 private IEnumerator TimerRoutine()
 {
      //code can be executed anywhere here before this next statement 
      yield return new WaitForSeconds(5); //code pauses for 5 seconds
     //code resumes after the 5 seconds and exits if there is nothing else to run
 
 }

I recommend looking into Coroutines because you can do a lot with them.

Comment
Add comment · Show 2 · 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 akaBl4ck · Sep 01, 2021 at 01:43 PM 0
Share

Hey. it doesn't work, it starts the corountine every second.

avatar image tbriz akaBl4ck · Jan 09 at 08:25 PM 0
Share

@akaBl4ck check my answer below, proper way to use coroutine cooldown.

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

16 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

Related Questions

A node in a childnode? 1 Answer

Timer help please 1 Answer

PLease Help 1 Answer

Rotate Object from Other Local Object 2 Answers

How can i find to Cool Main menu for iOS 1 Answer


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