Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. public class YourCoolMonobehaviour : Monobehaviour
  2. {
  3.     [SerializeField] private float cooldownTime = 3.0f;
  4.     private CoolDownInformation cooldownMana;
  5.  
  6.     private void Start()
  7.     {
  8.         cooldownMana = new CoolDownInformation(cooldownMana);
  9.     }
  10.    
  11.     private void Update()
  12.     {
  13.         // The "IsElapsed"-function has to run every frame
  14.         if (Input.GetButtonDown("Fire2") && cooldownMana.IsElapsed(Time.deltaTime))
  15.         {
  16.             //Do your Mana Stuff
  17.         }
  18.  
  19.         // If this is not possible, use "IncreaseByDeltaTime"-function to increase the elapsed time in the structure and the "IsElapsed(bool resetAfterElapsed = true)" for checking
  20.         cooldownMana.IncreaseByDeltaTime(Time.deltaTime);
  21.         if(sometimestrue)
  22.         {
  23.             if (Input.GetButtonDown("Fire2") && cooldownMana.IsElapsed())
  24.             {
  25.                 //Do your Mana Stuff
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31. [System.Serializable]
  32. public struct CoolDownInformation
  33. {
  34.     public float Cooldown;
  35.     public float elapsedTime;
  36.  
  37.     private bool cooldownOnStart;
  38.  
  39.     public CoolDownInformation(float cd, bool cooldownOnStart = false)
  40.     {
  41.         this.Cooldown = cd;
  42.         this.elapsedTime = cooldownOnStart ? 0.0f : (cd + 1.0f);
  43.         this.cooldownOnStart = cooldownOnStart;
  44.     }
  45.  
  46.     public bool IsElapsed(float deltaTime, bool resetAfterElapsed = true)
  47.     {
  48.         if (!this.cooldownOnStart)
  49.         {
  50.             this.elapsedTime += deltaTime;
  51.             bool state = this.elapsedTime >= this.Cooldown;
  52.  
  53.             if (resetAfterElapsed)
  54.             {
  55.                 this.elapsedTime = 0.0f;
  56.                 return state;
  57.             }
  58.             else
  59.             {
  60.                 return state;
  61.             }
  62.         }
  63.         else
  64.         {
  65.             this.cooldownOnStart = false;
  66.             return true;
  67.         }
  68.     }
  69.  
  70.     public bool IsElapsed(bool resetAfterElapsed = true)
  71.     {
  72.         if (!this.cooldownOnStart)
  73.         {
  74.             this.elapsedTime += deltaTime;
  75.             bool state = this.elapsedTime >= this.Cooldown;
  76.  
  77.             if (resetAfterElapsed)
  78.             {
  79.                 this.elapsedTime = 0.0f;
  80.                 return state;
  81.             }
  82.             else
  83.             {
  84.                 return state;
  85.             }
  86.         }
  87.         else
  88.         {
  89.             this.cooldownOnStart = false;
  90.             return true;
  91.         }
  92.     }
  93.  
  94.  
  95.     public void IncreaseByDeltaTime(float deltaTime)
  96.     {
  97.         this.elapsedTime += deltaTime;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement