Guest User

BoolTimer

a guest
Aug 10th, 2022
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. /// <summary>
  2. /// Sets a bool to true for a certain amount of time, then resets
  3. /// </summary>
  4. public struct BoolTimer
  5. {
  6.     float resetTime;
  7.  
  8.     /// <summary>
  9.     /// Set the value to true for time seconds
  10.     /// </summary>
  11.     public void Set(float time)
  12.     {
  13.         resetTime = Mathf.Max(resetTime, Time.time + time);
  14.     }
  15.  
  16.     /// <summary>
  17.     /// Set the value to true for time seconds
  18.     /// </summary>
  19.     public void Set(float time, bool overwrite)
  20.     {
  21.         if(overwrite)
  22.             resetTime = Time.time + time;
  23.         else
  24.             resetTime = Mathf.Max(resetTime, Time.time + time);
  25.     }
  26.  
  27.     /// <summary>
  28.     /// Set the value to false and reset timer
  29.     /// </summary>
  30.     public void Reset()
  31.     {
  32.         resetTime = Time.time - 1;
  33.     }
  34.  
  35.     public bool Value { get { return Time.time <= resetTime; } }
  36.  
  37.     public static implicit operator bool(BoolTimer bt)
  38.     {
  39.         return bt.Value;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment