Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question About PlayerInput Component

Discussion in 'Input System' started by EliasMasche, Mar 7, 2021.

  1. EliasMasche

    EliasMasche

    Joined:
    Jul 11, 2014
    Posts:
    92
    Hello users from Unity Forum InputSystem.
    Started using the new Input system and i have some doubts as a new developer with zero to junior experience, so searching about more information, recommendations or differences how to use it 'properly' or 'effective'.
    Watching on some tutorials from youtube as the documentation saw some devs use PlayerInput Component that InputSystem provides and others create from zero in a script and call them 'Input Manager'.

    Checking the repository of InputSystem_Warriors, the example are using PlayerInput component but using a PlayerController and from there is getting called to the correspondent Methods in the different SubBehaviors as is called.
    Right now as experimenting i am not using PlayerInput, created a script a singleton instance called 'InputManager' where i create public methods for reading the values/input from InputAsset/Actions and this methods are called in others scripts for getting this values majority of them void Update().

    Example:
    InputManager
    Code (CSharp):
    1. public class PlayerInputManager : MonoBehaviour
    2. {
    3.     private PlayerControlInput m_Controls;
    4.     public Vector2 OnMovePerformed()
    5.     {
    6.         return m_Controls.Gameplay.Movement.ReadValue<Vector2>();
    7.     }
    8.     public bool OnJumpTriggered()
    9.     {
    10.         return m_Controls.Gameplay.Jump.triggered;
    11.     }
    12.     public bool OnJumpButtonUp()
    13.     {
    14.         if (m_Controls.Gameplay.Jump.WasReleasedThisFrame())
    15.         {
    16.             m_JumpState = false;
    17.         }
    18.         return m_JumpState;
    19.     }
    20. }
    PlayerController
    Code (CSharp):
    1.  public class PlayerController : MonoBehaviour
    2. {
    3.     private PlayerInputManager m_PlayerInputManager;
    4.     private void Start()
    5.     {
    6.         m_PlayerInputManager = PlayerInputManager.Instance;
    7.     }
    8.     private void Update()
    9.     {
    10.     m_MoveDirection = m_PlayerInputManager.OnMovePerformed();
    11.     m_MoveInput = new Vector3(m_MoveDirection.x, 0, m_MoveDirection.y);
    12.     QueueJump();
    13.     }
    14.  
    15.     private void QueueJump()
    16.     {
    17.     if (m_AutoBunnyHop)
    18.     {
    19.         m_JumpQueued = m_PlayerInputManager.OnJumpTriggered();
    20.         return;
    21.     }
    22.  
    23.     if (m_PlayerInputManager.OnJumpButtonDown() && !m_JumpQueued)
    24.     {
    25.         m_JumpQueued = true;
    26.     }
    27.  
    28.     if (m_PlayerInputManager.OnJumpButtonUp())
    29.     {
    30.         m_JumpQueued = false;
    31.     }
    32.   }
    33. }
    So far i feels that my InputManager will be blobbed with new methods for every new Input i add for my player as i have the sensation doing something wrong, that are better ways, are benefits doing this method or i just readapt to use PlayerComponent, etc.
    Thanks for reading and hoping for info about more experienced developers.