- Home /
How to initiate global cooldown?
Hi, so i am making a third person "shooter" type game where the player character has multiple abilities. What i want, is to be able to set off a global cooldown whenever any on those abilities is activated. For example i activate the ability nr 1. and every ability suffers a cooldown of 0.5 seconds plus the ability that was activated suffers 1.5 seconds more cooldown(so 2 seconds total for the ability nr 1). I have made seperate scripts for every ability and each one of those scripts has the same function "function Cooldown()" in them. Should i merge the scripts or can i make this happen another way?
`function LateUpdate () {
ShootFireBall();
Cooldown(cooldown);
if(!gameObject.GetComponent(ShootFrostBolt).onCooldown){
Debug.Log("Frostbolti cooldown");
gameObject.GetComponent(ShootFrostBolt).Cooldown(1);
}
}
function Cooldown(cooldown:float) {
timer += Time.deltaTime;
if(timer >= cooldown) timer = cooldown;
if(timer < cooldown) onCooldown = true;
else onCooldown = false;
return onCooldown;
}`
and a similar piece of code on the ShootFrostBolt script.
You have a typo in the very subject line of your question. You can click "Edit" to fix it.
Answer by aldonaletto · Jan 22, 2012 at 04:04 PM
You could merge all ability scripts in a single code, or use a common script (an already existing player script, or a new one) with the code below:
// define an identifier for each ability (Size must be the last enum // item: it's just a trick to know how much abilities were defined): enum Abilities { FireBall, FrostBolt, OtherThing, Size };
var cooldownTimes = new float[Abilities.Size]; var regularTime: float = 0.5; var extraTime: float = 1.5;
function SetCooldown(ab: Abilities){ for (i = 0; i < Abilities.Size; i++){ var t = regularTime; // start with regular time if (ab == i) t += extraTime; // add extra time for selected ability if (Time.time < cooldownTimes[i]) // if already in cooldown time... t += cooldownTimes[i]; // extend it... else t += Time.time; // else start the new cooldown time cooldownTimes[i] = t; } }
function OnCooldown(ab: Abilities): boolean { return Time.time < cooldownTimes[ab]; } Supposing the code above is in a script called "Cooldown.js", you could use something like this in each ability script:
var cooldownScript: Cooldown;
function Start(){ // get a reference to the Cooldown script at Start cooldownScript = GetComponent(Cooldown); }
// when activating some ability, do something like this: if (OnCooldown(Abilities.FireBall) == false){ // if not in cooldown... ShootFireBall(); // use the ability... // and set all cooldown timers at once: cooldownScript.SetCooldown(Abilities.FireBall); } Even if you decide to merge the scripts, the first code sample may be used to control the cooldown times - just omit the cooldownScript. part and the GetComponent at Start.
Thanks for replying! I think i can figure it out by myself now :) (if i cant ill ask for your help once again tho :D)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Brackeys Inventory System doubts... 2 Answers
Assign a MonoBehaviour script to a variable. 2 Answers
Rotate Object from Other Local Object 2 Answers
Usage timer/weapon cooldown... 3 Answers