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 GreenZone · Dec 27, 2014 at 08:04 AM · c#addcooldown

Adding cooldown C#

Hi :D I am new :D And, I'm having trouble adding the cooldown to my script :( using UnityEngine; using System.Collections;

 public class TreeLogic : MonoBehaviour {
 
     public GameObject[] items = new GameObject[1];
     public int health = 100;
 
     void Update()
     {
         if(health <= 0)
         {
             Destroy(gameObject);
         }
     }
 
     void Hit(int damage)
     {
         Vector3 position = new Vector3((Random.Range(0, 10) - 5) + transform.position.x, Random.Range (0, 3) + transform.position.y, (Random.Range(0, 10) - 5) + transform.position.z); 
 
         foreach(GameObject item in items)
         {
             if(item != null)
             {
                 Instantiate(item, position, Random.rotation);
             }
         }
 
         health -= damage;
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public class ToolAnimator : MonoBehaviour {
 
     public float maxDistance;
     public float damage;
     public RaycastHit hit;
 
     void Update()
     {
         if(Input.GetKeyDown (KeyCode.F))
         {
             if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out hit, maxDistance))
             {
                 print("Hit!");
                 hit.transform.SendMessage ("Hit", damage, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }
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
2

Answer by Mmmpies · Dec 27, 2014 at 09:51 AM

Hi, and welcome.

Create a float that records the start of your cool down with Time.time and another that holds how long your cool down time is.

Then check if the current Time.time is greater than the start + the length.

It's not clear what the cool down is for but let's say it's for a fire spell.

 private float fireSpellStart = 0f;
 private float fireSpellCooldown = 2f; // 2 = two seconds 
 
 // then where you call the spell or whatever it is you call
 // check that Time.time is greater than fireSpellStart
 // + fireSpellCooldown
 // and if it is allow that spell to be cast but record the start time
 
 if (Time.time > fireSpellStart + fireSpellCooldown)
 {
     fireSpellStart = Time.time;

If you want to record how long you have left the have another variable that when you start the cool down is set to fireSpellCooldown and then in update is reduced by Time.deltaTime each frame until

Of course you can make this more complex if you have hundreds of different spells or attacks that you want to have different cool down times for but this should give you an idea of what to do.

Worth looking up Time.time and Time.deltaTime as you'll likely use them a lot.

Hope that helps.

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 isteffy · Sep 06, 2015 at 12:44 PM 0
Share

Using your code, I trigger this if(){} conditional you have on collisionEnter, however fireSpellStart = Time.time; is called immediately regardless if fireSpellCooldown is set to 9999f;

Time.time begins when the game starts and counts upwards until the game stops. If fireSpellStart + fireSpellCooldown = 2f, then Time.time will always be greater if the game has continued longer than 2 seconds.

Should fireSpellStart be set to = Time.time first?

avatar image PlatPlayz · Mar 16, 2018 at 11:35 AM 0
Share

Well, Bump.

avatar image
0

Answer by majervojtik · Apr 30, 2021 at 09:58 AM

can anybody help it doesnt work

public GameObject platform; public float platformStart = 0f; public float cooldown = 4f;

 void Update()
 {



     if (Input.GetMouseButton(0))
     {
         if (Time.time >= platformStart + cooldown)
         {
             platformStart = Time.time;
             Vector3 mousePos = Input.mousePosition;
             mousePos.z = 2.0f;
             Vector3 objectPos = Camera.current.ScreenToWorldPoint(mousePos);
             Instantiate(platform, objectPos, Quaternion.identity);

         }
Comment
Add comment · Show 1 · 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 Hellium · Apr 30, 2021 at 10:16 AM 0
Share

What "does not work"? Do you have any error in the console? Are you sure cooldown is greater than 0 IN THE INSPECTOR?

avatar image
0

Answer by FaffyWaffles · May 01, 2021 at 01:16 PM

Honestly, the straight up easiest and best way to do cooldowns is to use a coroutine.

 public class CooldownControllerExample : MonoBehaviour
 {
     public float attackCooldown;
     private bool cool = true;
 
     public void CooldownStart()
     {
         StartCoroutine(CooldownCoroutine());
     }
     IEnumerator CooldownCoroutine()
     {
         cool = false;
         yield return new WaitForSeconds(attackCooldown);
         cool = true;
     }
 }

Whenever you would want to start the cooldown, all you would have to do if call CoolDownStart();, and be checking the bool. if bool is true, you are cool ;)

Comment
Add comment · Show 1 · 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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to add values that are in a Text array? 1 Answer

C# Script not running at all. 0 Answers

How to add 2 Quaternions. 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