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 jetty112 · Jan 22, 2012 at 03:22 AM · globalcooldownspell

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.

Comment
Add comment · Show 1
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 Fattie · Jan 22, 2012 at 11:41 AM 0
Share

You have a typo in the very subject line of your question. You can click "Edit" to fix it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

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 jetty112 · Jan 22, 2012 at 04:10 PM 0
Share

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)

avatar image aldonaletto · Jan 22, 2012 at 04:17 PM 0
Share

You're welcome!

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

6 People are following this question.

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

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


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