How to perform only once action when a condition is met?

I’m new to Unity, and Learning C# along with it. It’s going stellar, this is significantly easier than I thought it would be. I’m going to be as clear as I possibly can, because I’ve noticed a lot of people on this forum don’t make their problem clear enough, and that just makes things confusing for everyone.

There’s one thing I can’t seem to find however. In other languages I’ve used, there’s usually some sort of “Only one action when event loops”, or “Trigger Once” type event. For example, if you have it so that your character makes a sound effect when he drops off a platform by checking if he is in the air, it doesn’t play the sound effect 6 million times until he lands.

With what I’m working with right now, I have it so once you tap your joystick at a specific speed, your character gets put into a “Dash” action, where there is a countdown timer to tell the character how long it should be in “Dash”. The problem is that once the condition to Dash is met, the timer will stay at what I set it to, because it’s updating every frame. I need it so that the event is only tiggered once, so the DashTimer can subtract like I have it set to do. Any ideas? I’ll write some crappy pseudo code modeled after another language I know that has this function:

-KeyRight( “Player_Input” ) == 1
Set Action( “Player” ) to “Walk”

All this does is set the player’s action to Walk when you press right, and it keeps it that way.

-Only One Action when Event Loops
-KeyRight( “Player_Input” ) == 1
PlaySound.( “Step” )

This one is different. When you press right, it plays the step sound effect, but only once. This is what I’m going for.

Any help is greatly appreciated!

I’m not exactly sure what you want (maybe couse I’m beginner) but if it’s about playing audio once, I remember using that function from tutorials here PlayOneShot - it played audio clip only once. If you wanted something regarding that timer and conditions I guess it would be easier for people if you put your code in here. Hope it helps :slight_smile:

Maybe something like this.

SpecialAbility.cs

using UnityEngine;

public class SpecialAbility : MonoBehaviour
{
    public float useDuration = 30.0f;
    public float rechargeDuration = 60.0f;

    private float useEnd;
    private float rechargeEnd;

    public bool IsUsing
    {
        get;
        private set;
    }


    public bool CanUse
    {
        get;
        private set;
    }

    private void Update()
    {
        this.IsUsing = Time.time < this.useEnd;
        this.CanUse = Time.time > this.rechargeEnd;
    }

    public void Use()
    {
        if (this.CanUse)
        {
            this.useEnd = Time.time + this.useDuration;
            this.rechargeEnd = this.useEnd + this.rechargeDuration;
        }
    }
}