How to make KeyPressDown & KeyPressUp in new Unity system

Viewed 33

I want to create a button which when clicked will turn a value true and start my function once. But for the past 4 days, it just doesn't want to work and it legit makes me want to just scratch and use the old system even though it will cause more trouble for me in the long run so can anyone give me an idea of how to do it correctly? Like this is the basic idea I want but all post and documentation makes it more confusing on how to achieve.

bool temp = false;

void IsInteracting
{
    temp = !temp;
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        Debug.Log("You clicked: " + IsInteracting);
    }

    if (Input.GetKeyUp(KeyCode.E))
    {
        Debug.Log("You clicked: " + IsInteracting);
    }
} 

Output:

You clicked:True   <<  **Happened JUST ONCE**
You clicked:False  <<  **Happened JUST ONCE**

I want this logic in my project with my buttons but it just does a spam of the button being multiple time calling the script which breaks it.

I tried Hold interaction with either an value checking if the threshold I over or under the correct value. Doesn't work.

I tried press and release which double spams the function when clicking or unclicking and spams function calling.

I tried even doing an Axis which checks the amount button is being held which again, just spams function.

I could make an bool which forces the script to run once but that's just a bandaid which won't hold if I add more functions.

If anyone wonder's this the code I attempted to make.

input.KeyBoard.Interact.performed += ctx =>
    {
              interactionInput.InteractedClicked = ButtonTriggerd();
        Debug.Log(ButtonTriggerd() + "Click");
    };

input.KeyBoard.Interact.canceled += ctx =>
    {
        interactionInput.InteractedClicked = ButtonTriggerd();
        Debug.Log(ButtonTriggerd() + "UnClick");
    };

buttonTriggeris() is just a switch boolean function.

0 Answers
Related