Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Decreasing value gradually in non-monobehaviour class

Discussion in 'Scripting' started by Dino1997, May 30, 2014.

  1. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    Hello everyone,

    I'm working on a spell casting system,I have created base Spell class that doesn't inherit from anything so I don't have Update() function.
    I have two variables that I use for cooldown

    Code (csharp):
    1. private float _cooldownTime;
    2. private float _cooldownTimer;
    now I'm wondering if there is a way to decrease my cooldown timer if it is bigger than zero,basically calling this function every frame

    Code (csharp):
    1. if(CooldownTimer > 0) {
    2.     CooldownTimer -= Time.deltaTime;
    3.     Debug.Log(CooldownTimer);
    4. }
    Thanks
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Generally, we have spell data in a C# class. I'm guessing there is something in the game that is tracking spells that are being cast. That SpellTracker class will either need to extend MonoBehaviour, or some MonoBehaviour class is going to have to run the SpellTracker on it's Update() cycle.
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  4. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    SpellTracker is a good idea,thanks :D
     
  5. Deleted User

    Deleted User

    Guest

    The question is. Do you really need the spell class to continuously count? Most of the time you don't, instead you can compare the last time the spell was used, against the current time.

    Example code, untestet and typed right inside browser

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Spell
    5. {
    6.     // the stored time the spell was last used
    7.     private float _lastTimeUsed;
    8.  
    9.     private float _coolDownTime;
    10.  
    11.     public Spell()
    12.     {
    13.         // set the last timne used to the negative value of cool down time
    14.         // this way we can cast the spell right away
    15.         _lastTimeUsed = -_coolDownTime;
    16.     }
    17.  
    18.     public bool CanCast()
    19.     {
    20.         return Time.time >= _lastTimeUsed + _coolDownTime;
    21.     }
    22.  
    23.     public void CastSpell()
    24.     {
    25.         // if we can't cast the spell return, else "reset" time
    26.         if( CanCast() == false )
    27.             return;
    28.         else
    29.             _lastTimeUsed = Time.time;
    30.  
    31.         // Do Work
    32.     }
    33.  
    34. }
    35.  
    36.  
    37.  
     
  6. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    That is a very good idea :D Thank you!