Input system

 avatar
unknown
csharp
a year ago
3.3 kB
21
Indexable
using UnityEngine;
using UnityEngine.InputSystem;

public class InputManager : MonoBehaviour
{
    public Vector2 MoveInput { get; private set; } = Vector2.zero;
    public bool JumpHeld { get; private set; } = false;
    public bool JumpDown { get; private set; } = false;
    public bool RunInput { get; private set; } = false;
    public bool CrouchInput { get; private set; } = false;
    public bool CrawlInput { get; private set; } = false;

    public InputMap Input {get; private set;}
    
    public static InputManager Instance { get; private set; }
    
    private void Awake()
    {
        if (Instance != null)
        {
            Debug.LogWarning("There is more than one instance of InputManager");
            return;
        }
        Instance = this;
    }

    //Subscribe to the input system
    private void OnEnable()
    {
        Input = new InputMap();

        Input.PlayerInput.Enable(); //Enable the player input actions

        //Let know when the button is still pressed
        //Get the reference to the SetMovement method and not call it everytime
        Input.PlayerInput.MovementInput.performed += SetMovement;
        //if the input is canceled, set the movement to zero
        Input.PlayerInput.MovementInput.canceled += SetMovement;
        
        //Using canceled to also call when the button is released
        Input.PlayerInput.RunInput.started += SetRun;
        Input.PlayerInput.RunInput.canceled += SetRun;
        
        Input.PlayerInput.CrawlInput.started += SetCrawl;
        Input.PlayerInput.CrawlInput.canceled += SetCrawl;
        
        Input.PlayerInput.CrouchInput.started += SetCrouch;
        Input.PlayerInput.CrouchInput.canceled += SetCrouch;

    }

    //Subscribe to the input system
    private void OnDisable()
    {
        Input.PlayerInput.MovementInput.performed -= SetMovement;
        Input.PlayerInput.MovementInput.canceled -= SetMovement;
        
        Input.PlayerInput.RunInput.started -= SetRun;
        Input.PlayerInput.RunInput.canceled -= SetRun;
        
        Input.PlayerInput.CrawlInput.started -= SetCrawl;
        Input.PlayerInput.CrawlInput.canceled -= SetCrawl;
        
        Input.PlayerInput.CrouchInput.started -= SetCrouch;
        Input.PlayerInput.CrouchInput.canceled -= SetCrouch;

        Input.PlayerInput.Disable(); //Enable the player input actions
    }

    private void Update()
    {
        JumpHeld = Input.PlayerInput.JumpInput.IsPressed();
        JumpDown = Input.PlayerInput.JumpInput.WasPressedThisFrame();
    }

    void SetMovement(InputAction.CallbackContext context)
    {
        MoveInput = context.ReadValue<Vector2>();
    }
    
    private void SetRun(InputAction.CallbackContext context)
    {
        //Check if the button is pressed or activated
        RunInput = context.started;
    }
    
    private void SetCrouch(InputAction.CallbackContext context)
    {
        //Check if the button is pressed or activated
        CrouchInput = context.started;
    }
    
    private void SetCrawl(InputAction.CallbackContext context)
    {
        //Check if the button is pressed or activated
        CrawlInput = context.started;
    }

}
Editor is loading...
Leave a Comment