Input persistant

Hi everyone,

I have trouble with implementation of input method.
I want to use a var “est_clik” in a fonction and this var must be turn “true” when user click on joystick.
So a part of my code:

    public void Update() { //vérifie le D-Pad pour faire avancer les actions
        est_clik = false;
        if (Input.GetKey(KeyCode.Joystick1Button0)) { //press entter
            est_clik = true;
          //  Debug.Log("Estclick true ");
        }

The problème is “Input.GetKey(KeyCode.Joystick1Button0)” still true when I finished to click joystick. So my var stay always true.
Have ou a solution to “flush” Input…? To make Input.GetKey(KeyCode.Joystick1Button0) false when the joystick is released.

Thank you.

Not exactly sure as to what you mean by it staying always true, as I think with the code above it is impossible. Maybe you meant it is staying true for multiple frames, where you expected only one frame?
Try Input.GetKeyDown

1 Like

Hi, thanks for your answers I think it’s exactly what I want but It didn’t work. Input.GetKeyDown(KeyCode.Joystick1Button0) stay true frame after frame…

I have add

Input.ResetInputAxes();

But same behaviour.

Any solution …?

public void Update() { //vérifie le D-Pad pour faire avancer les actions
        est_clik = false;
        Input.ResetInputAxes(); // marche pas il faut trouver une solution
        if (Input.GetKeyDown(KeyCode.Joystick1Button0)) { //press enter
            est_clik = true;
            Debug.Log("Estclick true ");
        }

        foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKey(vKey))
            {
                print("***********************" + vKey.ToString());

            }
        }

    }

I am not sure if ResetInputAxes affects GetKey, but I dont think thats the issue anyways.
I cant test with joystick, but I recreated your code and the est_Clik is only logging once as expected.
Is it the code in the foreach that is causing you trouble now? Because you have GetKey, not GetKeyDown in there, so it will spam.

Does this code work for you?
Click for code

using UnityEngine;

public class TestInput : MonoBehaviour
{
    bool clicked;

    void Update()
    {
        clicked = false;
        if(Input.GetKeyDown(KeyCode.A)) //Press A
        {
            clicked = true;
            Debug.Log("Clicked!");
        }

        foreach(KeyCode key in System.Enum.GetValues(typeof(KeyCode)))
        {
            if(Input.GetKeyDown(key))
            {
                Debug.Log("*************** " + key.ToString());
            }
        }
    }
}

Is the Debug.Log(“Clicked!”); running multiple times, or is it just that your est_Clik variable is staying true? Does anything else have a reference to that variable that could be setting it to true?
I am assuming you have that est_Clik variable doing something somewhere else, otherwise its pointless to have it. Maybe post the rest of the code?

1 Like