Git Product home page Git Product logo

cooldownapi's Introduction

img

Content

Setup And Examples

Let's say we have an attack method in our game for the player. When the player presses a key, we want to attack. But we also want to apply a cooldown when the player attacks to prevent the player from attacking too quickly.

First let's declare a Cooldown for the attack and call it "attackCooldown"
Also, let's declare a float variable for the amount of seconds between attacks. Let's say we want the duration of the cooldown to be 3.25 seconds.

float attackCoodldownDuration = 3.25f;
Cooldown attackCooldown;

Now to initialize it. All you got to to is to call its constructor.
When you initialize a Cooldown object, you set its default duration value.
And it's the float variable we declared before.
Make sure you initialize your cooldowns inside Start() or Awake()

void Awake()
{
  attackCooldown = new Cooldown(attackCooldownDuration);
}

Done. The cooldown is ready.
Let's activate it when we attack. We just need to use the method Activate()

void Attack()
{
  // some attack behaviour
  ...
  // cooldown activation
  attackCooldown.Activate();
}

Now, let's prevent the player from attacking if the cooldown is active.
We can do this by checking the value IsActive in the cooldown.

if (//player is pressing the attack button)
{
  if (attackCooldown.IsActive == false)
  {
    Attack();
  }
}

A Cooldown object contains two properties. A timer and a default duration.
The Cooldown is considered "active" when its timer equal to zero.
The timer is always decreasing until it reaches zero.
Let's say we want to add a special, shorter attack, and apply a short cooldown for it with a different duration.
We can do it with the Activate() method by inputting a custom duration to it.

void SpecialAttack()
{
  // some attack behaviour
  ...
  // cooldown activation
  attackCooldown.Activate(1.7f); // cooldown applied with a custom duration.
}

You can also change the default duration at any time with the method ChangeDuration()

attackCooldown.ChangeDuration(8f);

You can also manually deactivate the cooldown and reset its timer to zero.
Use the method Deactivate()

attackCooldown.Deactivate();

Documentations

Cooldown

A class for handling a cooldown in Unity using a timer.

  • Properties

    • IsActive
      Determines if the cooldown is currently active.
      Returns true if the timer is higher than zero.

    • Duration
      Returns the default duration value of the Cooldown object.
      The default duration is set upon initialization.
      It used when you call the Activate() method without inputting parameters.

    • Timer
      Returns the current value from the cooldown's timer

  • Methods

    • Activate()
      Activates the cooldown with the default duration.
      You can get the default duration's value form the Duration propertie.

    • Activate(float customDuration)
      Activates the cooldown with a custom duration.

    • Deactivate()
      Deactivates the cooldown manually.
      Resets the timer's value to zero.

    • ChangeDuration(float duration)
      For changing the default duration's value.

  • Constructors

    • Cooldown(float duration)
      Used for creating a new cooldown
      Input the default duration of the cooldown
  • Events

    • BecameInactive
      An event that is raised when the cooldown becomes inactive

Importing

Click here to download, or go to the packages folder of the repository and download the package file.
Then import the package directly to your project like any other Unity package. This is he fastest and easiest way.
Done!

cooldownapi's People

Contributors

jozzzzep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

r2d2m

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.