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.

[SOLVED] New Input System: if binding is been hold down call a method

Discussion in 'Input System' started by davidbasilefilho, Mar 21, 2021.

  1. davidbasilefilho

    davidbasilefilho

    Joined:
    May 17, 2020
    Posts:
    6
    How can i do this, like the old Input.GetKey() on Update()
     
    Last edited: Mar 21, 2021
  2. CreakyVibez

    CreakyVibez

    Joined:
    Feb 22, 2021
    Posts:
    6
    I want to know as well
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,816
  4. davidbasilefilho

    davidbasilefilho

    Joined:
    May 17, 2020
    Posts:
    6
    i'm using the input actions asset and scriptableobjects, but i figured it out.


    Code (CSharp):
    1. public class InputReader : ScriptableObject, YourInputClass.ISomethingActions{
    2.  
    3.       // needs UnityEngine.Events namespace
    4.       public event UnityAction<bool> CallFrameEvent = delegate { };
    5.  
    6.       bool state;
    7.  
    8.       public OnCall(InputAction.CallbackContext context){
    9.             state = !state;
    10.             OnCallFrameEvent?.Invoke(state);
    11.       }
    12.  
    13. }
    Code (CSharp):
    1. //in your script
    2.  
    3. [SerializeField] InputReader input;
    4.  
    5. bool holding;
    6.  
    7. void Awake(){
    8.     input.OnCallFrameEvent += EventListener;
    9. }
    10.  
    11. void Update(){
    12.     if(holding){
    13.         //do something
    14.     }
    15. }
    16.  
    17. void EventListener(bool holding){
    18.      this.holding = holding;
    19. }
    make sure you have the the press interaction with the press and release trigger behaviour
     
    Last edited: Mar 22, 2021
    CreakyVibez likes this.
  5. davidbasilefilho

    davidbasilefilho

    Joined:
    May 17, 2020
    Posts:
    6
    the code was incorrect, i fixed it now
     
    CreakyVibez likes this.