Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved New Input System equivalent of GetButton boolean

Discussion in 'Input System' started by MonoFlux, Dec 21, 2022.

  1. MonoFlux

    MonoFlux

    Joined:
    Jun 13, 2015
    Posts:
    39
    Hi, I have a script that needs to get a boolean value when a button is pressed in the update method. In the old input system I would do something like this:

    Code (CSharp):
    1.  
    2. bool jump = false;
    3.  
    4. void Update()
    5. {
    6.     jump = Input.GetButton("Jump");
    7. }
    8.  
    However, on the new input system, I have to use the event that fires instead:
    Code (CSharp):
    1.  
    2. public void OnJump(InputValue value)
    3. {
    4.     jump = true;
    5. }
    6.  
    I understand that the logic relating to the button press can be handled in the event, but if I need to use the boolean in the update method, how can I reset the boolean for the frames where the button is not pressed?

    Hope that makes sense. thanks!
     
  2. MonoFlux

    MonoFlux

    Joined:
    Jun 13, 2015
    Posts:
    39
    Ok, so this post was really helpful:

    https://forum.unity.com/threads/equivalent-of-input-getbutton-in-new-input-system.1161746/

    I needed to get a reference to the action:

    Code (CSharp):
    1. var map = CurrentInputControl.currentActionMap; //CurrentInputControl is exposed in editor to drag in the Player Input
    2. inputJump= map.FindAction("Jump", true);
    Then in my Update, I can use the IsPressed() method which is similar to GetButton:

    Code (CSharp):
    1. jump =  inputJump.IsPressed();