Search Unity

Question How to poll PlayerInput Component like the "Input Action C# class"?

Discussion in 'Input System' started by Peter77, Aug 23, 2022.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    I want to use the PlayerInput Component to query for input, but I can't figure out how to poll whether a certain action/button is pressed.

    I don't want to use event callbacks, because it makes the code unnecessarily complex in my case. I want to continue to use the "Input Action C# class" that the Input System generates for me.

    With the "Input Action C# class", I can write code like this:
    Code (CSharp):
    1. myActions.Player.Move.ReadValue<Vector2>()
    I want to do the same when using the PlayerInput Component. My question is, how can I use the "Input Action C# class" in conjunction with the PlayerInput Component?

    My current approach looks like this:
    Code (CSharp):
    1. public class HeroInput : MonoBehaviour
    2. {
    3.     [SerializeField] PlayerInput m_PlayerInput;
    4.  
    5.     InputAction m_Move;
    6.     InputAction m_Slide;
    7.  
    8.     void Awake()
    9.     {
    10.         m_Move = m_PlayerInput.currentActionMap.FindAction("Move");
    11.         m_Slide = m_PlayerInput.currentActionMap.FindAction("Slide");
    12.     }
    13.    
    14.     void Update()
    15.     {
    16.         if (Mathf.Abs(m_Move.ReadValue<Vector2>().x) > 0)
    17.             ...
     
    Last edited: Aug 23, 2022