activate script after x amount of pressed button

hey everyone

so I have a scene hooked up with my xbox controller where smth happens when you push down on joystick button14.

I want that a shader on the FPS camera activates after button14 has been pressed 30 times.

How do I go about that ? Did anyone try out smth similar?

looking forward hearing back from you

all the bests
Line

Hi @linelade,

To do this you will need to keep track of how many times the player has pressed the button. I have written a little script that will do that for you. Currently, it also has a maximum amount of time between the button presses. If you don’t want this just remove the region “Time related”

If you only want it to activate once and then not be activated again set the bool “OnlyActivateOnce” to true.


[SerializeField] private int requiredButtonActivations = 30; //Amount of times the button has to be pressed before activating RequirementsMet()
private int buttonActivations = 0; //Amount of times the button has been pressed.

[SerializeField] private bool onlyActivateOnce = false;
private bool hasBeenActivated = false;

#region TimeRelated


[SerializeField] private float timeFrame; //This float represents the max amount of time between button presses.
private float elapsedTime = 0; //Elapsed time between button presses. 

void Update()
{
    if(buttonActivations > 0)
    {
        if(elapsedTime < timeFrame) //Elapsed time is still within acceptable range. Keep counting up.
        {
            elapsedTime += Time.deltaTime;
        }
        else if(elapsedTime => timeFrame) //Player did not press button again within given timeframe. Reset.
        {
            buttonActivations = 0;
            elapsedTime = 0;
        }
    }
}
#endregion

//Call this void when button is pressed
public void OnButtonPressed()
{
    if(!hasBeenActivated)
    {
        if(buttonActivations < requiredButtonActivations)
        {
            elapsedTime = 0;
            buttonActivations += 1;    
        }   

        if(buttonActivations >= requiredButtonActivations)
        {
            buttonActivations = 0;
            elapsedTime = 0;

            if(onlyActivateOnce)
            {
                hasBeenActivated = true;
            }

            RequirementsMet();
        }
    }
    
}

void RequirementsMet()
{
    //Do whatever you want to happen after the 30 presses.
}

I hope this answers your question! If you have any other (Followup) questions feel free to ask!

Best,

Wybren van den Akker,

Unity Support

@WybrenAkker @Kitticid3

thanks a lot to the both of you! script is now up and running and after 30 presses I have an image effect on the camera coming up :slight_smile: