- Home /
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);
}
}
}
}
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.
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?
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);
}
What "does not work"? Do you have any error in the console? Are you sure cooldown
is greater than 0 IN THE INSPECTOR?
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 ;)
Your answer
Follow this Question
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