Input: Turning GetAxis into a GetButtonDown

So the bumper buttons on the xbox controller are axis, so if you want to turn them into a “fire” button you need to check the value of the “Fire” axis that the button 6 or 8 are mapped to.

The traditional way of doing GetButtonDown for axis is to have an Update loop sitting somewhere in your scene. I’d like to minimize the number of manager that my game logic depends on so Is there a way to do that without an Update loop, with events?

Spaghetti code to the rescue.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputX
{
    static bool fireDown, firePressed;
    static float timeLastFireDownQuerry, timeLastFireQuerry;

    public static bool IsFireButtonDown ()
    {
        if (Time.time == timeLastFireDownQuerry)
        {
            return fireDown;
        }
        else
        {
            timeLastFireDownQuerry = Time.time;
            if (fireDown)
            {
                fireDown = false;
            }
            else if (Input.GetButton ("Fire") || Input.GetAxis ("Fire") > .1f)
            {
                fireDown = true;
            }
            return fireDown;
        }
    }

    public static bool IsFireButtonPressed ()
    {
        IsFireButtonDown ();
        if (Time.time == timeLastFireQuerry)
        {
            return firePressed;
        }
        else
        {
            timeLastFireQuerry = Time.time;
            firePressed = Input.GetButton ("Fire") || Input.GetAxis ("Fire") > .1f;
            return firePressed;
        }
    }
}

Kinda late, but I’ve found a much faster solution:

        if(actionPressed)
            actionPressed = false;
        else if(actionAxis > 0f && actionAllow)
        {
            actionPressed = true;
            actionAllow = false;
        }
        if(actionAxis <= 0f)
            actionAllow = true;

actionAxis is you axis input and actionPressed is the GetButtonDown equivalent.