Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class YourCoolMonobehaviour : Monobehaviour
- {
- [SerializeField] private float cooldownTime = 3.0f;
- private CoolDownInformation cooldownMana;
- private void Start()
- {
- cooldownMana = new CoolDownInformation(cooldownMana);
- }
- private void Update()
- {
- // The "IsElapsed"-function has to run every frame
- if (Input.GetButtonDown("Fire2") && cooldownMana.IsElapsed(Time.deltaTime))
- {
- //Do your Mana Stuff
- }
- // If this is not possible, use "IncreaseByDeltaTime"-function to increase the elapsed time in the structure and the "IsElapsed(bool resetAfterElapsed = true)" for checking
- cooldownMana.IncreaseByDeltaTime(Time.deltaTime);
- if(sometimestrue)
- {
- if (Input.GetButtonDown("Fire2") && cooldownMana.IsElapsed())
- {
- //Do your Mana Stuff
- }
- }
- }
- }
- [System.Serializable]
- public struct CoolDownInformation
- {
- public float Cooldown;
- public float elapsedTime;
- private bool cooldownOnStart;
- public CoolDownInformation(float cd, bool cooldownOnStart = false)
- {
- this.Cooldown = cd;
- this.elapsedTime = cooldownOnStart ? 0.0f : (cd + 1.0f);
- this.cooldownOnStart = cooldownOnStart;
- }
- public bool IsElapsed(float deltaTime, bool resetAfterElapsed = true)
- {
- if (!this.cooldownOnStart)
- {
- this.elapsedTime += deltaTime;
- bool state = this.elapsedTime >= this.Cooldown;
- if (resetAfterElapsed)
- {
- this.elapsedTime = 0.0f;
- return state;
- }
- else
- {
- return state;
- }
- }
- else
- {
- this.cooldownOnStart = false;
- return true;
- }
- }
- public bool IsElapsed(bool resetAfterElapsed = true)
- {
- if (!this.cooldownOnStart)
- {
- this.elapsedTime += deltaTime;
- bool state = this.elapsedTime >= this.Cooldown;
- if (resetAfterElapsed)
- {
- this.elapsedTime = 0.0f;
- return state;
- }
- else
- {
- return state;
- }
- }
- else
- {
- this.cooldownOnStart = false;
- return true;
- }
- }
- public void IncreaseByDeltaTime(float deltaTime)
- {
- this.elapsedTime += deltaTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement