How you you wait for joystick button to come up???

So I have a menu that works with joystick buttons and axis. On the buttons I can do a getbuttonup. Otherwise the player will press and hold the button and fly though the menu screens. However, using the left or right axis, it works differently. I have it look for horizontal 1 to go to the next menu and horizontal -1 to go to last screen. The issue is if the player keeps the left or right axis button pressed down then when he gets to the next menu page, it see that it is -1 or 1 and changes pages again.

So how do I get the horizontal axis to accept the input but wait for the player to stop pressing it? Maybe a while loop waiting for 0? this has go to be an easy one.

(Sorry about this code but the website wont let me paste it using code. Yet another unity related bug).

using UnityEngine;
using System.Collections;

public class MenuP02Right : MonoBehaviour
{

///////////////////////////////////////////////////////////////
public MainMenu mm;
///////////////////////////////////////////////////////////////
void Update()
{
    //////////
    if (Input.GetAxisRaw("Horizontal") == 1)
    {
        mm.OnNextScreenP2Btn();
    }
}
///////////////////////////////////////////////////////////////

}

First, have you tried using GetButtonUp with the axis name? I never have, but maybe it emulates this sort of behavior. Worth a shot, otherwise…

The GetButtonUp function is basically looking for state changes. “Was the button down last frame and is it no longer down this frame? If so then was released this frame.” It’s fairly straightforward to simulate that functionality by keeping track of the last state and comparing the new state to the last.

Here’s some sample code. It’s not tested or even compiled so it’ll probably not work the first time, but hopefully it gets the concept across.

public class InputHelper : MonoBehaviour
{
    public float Sensitivity = 0.5f;

    bool wasRightPressed;
    bool wasLeftPressed;
    bool wasUpPressed;
    bool wasDownPressed;

    bool currentRightPressed;
    bool currentLeftPressed;
    bool currentUpPressed;
    bool currentDownPressed;

    public bool RightPressed { get { return currentRightPressed && !wasRightPressed; } }
    public bool RightReleased { get { return !currentRightPressed && wasRightPressed; } }

    public bool LeftPressed { get { return currentLeftPressed && !wasLeftPressed; } }
    public bool LeftReleased { get { return !currentLeftPressed && wasLeftPressed; } }

    public bool UpPressed { get { return currentUpPressed && !wasUpPressed; } }
    public bool UpReleased { get { return !currentUpPressed && wasUpPressed; } }

    public bool DownPressed { get { return currentDownPressed && !wasDownPressed; } }
    public bool DownReleased { get { return !currentDownPressed && wasDownPressed; } }
    

    void Update()
    {
        // get current horizontal state
        float currentState = Input.GetAxisRaw("Horizontal");
        currentRightPressed = currentState >= Sensitivity;   // you can tweak this value
        currentLeftPressed = currentState <= -Sensitivity;

        // get current vertical state
        currentState = Input.GetAxisRaw("Vertical");
        currentUpPressed = currentState <= -Sensitivity;    // pretty sure up is negative??
        currentDownPressed = currentState >= Sensitivity;   // but maybe not
    }


    void LateUpdate()
    {
        // remember the state for next time
        wasRightPressed = currentRightPressed;
        wasLeftPressed = currentLeftPressed;
        wasUpPressed = currentUpPressed;
        wasDownPressed = currentDownPressed;
    }
}

that gave me an idea of using a trigger. So here’s what I did and it works.’ Thanks for your help

using UnityEngine;
using System.Collections;
///////////////////////////////////////////////////////////////
public class MenuP01Right : MonoBehaviour
{
    ///////////////////////////////////////////////////////////////
    public MainMenu mm;
    bool triggerDown = false;
    ///////////////////////////////////////////////////////////////
    void Update ()
    {
        //////////
        if (Input.GetAxisRaw("Horizontal") == 1)
        {
            triggerDown = true;            
        }

        if (triggerDown && Input.GetAxisRaw("Horizontal") < 1)
        {
            triggerDown = false;
            mm.OnNextScreenP1Btn();
        }
        /////////
     }
    ///////////////////////////////////////////////////////////////
}