Hi everyone, I’m trying to implement my code for input system but being still a beginner I need a hand to understand. I attach my code that handles the mapped inputs.
For clarity first I would like to ask you if I am not overlooking anything in my code flow.
Second place where I’m finding complexity is in implementing correct input for the Perspective
action.
Generally this button will have to function as a simple button (pressed) i.e. (activates) so if released nothing happens, pressed again (activates) the function. If it is held down, it does not have to update the execution of the button (because it is not a hold down). There seems to be no management of this action in the input system, can anyone help me? -
P.S I’d like to add that I plan to implement gamepad actions as well.
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
[SerializeField] private PlayerInput PlayerInput;
[Header("Actions")]
[SerializeField] private Vector2 move;
[SerializeField] private Vector2 look;
[SerializeField] private bool sprint;
[SerializeField] private bool jump;
[SerializeField] private Vector2 zoom;
[SerializeField] private bool perspective;
public Vector2 Move { get { return move; } private set { move = value; } }
public Vector2 Look { get { return look; } private set { look = value; } }
public bool Sprint { get { return sprint; } private set { sprint = value; } }
public bool Jump { get { return jump; } private set { jump = value; } }
public Vector2 Zoom { get { return zoom; } private set { zoom = value; } }
public bool Perspective { get { return perspective; } private set { perspective = value; } }
private InputActionMap _currentMap;
private InputAction _moveAction;
private InputAction _lookAction;
private InputAction _sprintAction;
private InputAction _jumpAction;
private InputAction _zoomAction;
private InputAction _perspectiveAction;
private void Awake()
{
_currentMap = PlayerInput.currentActionMap;
_moveAction = _currentMap.FindAction("Move");
_lookAction = _currentMap.FindAction("Look");
_sprintAction = _currentMap.FindAction("Sprint");
_jumpAction = _currentMap.FindAction("Jump");
_zoomAction = _currentMap.FindAction("Zoom");
_perspectiveAction = _currentMap.FindAction("Perspective");
_moveAction.performed += onMove;
_lookAction.performed += onLook;
_sprintAction.performed += onSprint;
_jumpAction.performed += onJump;
_zoomAction.performed += onZoom;
_perspectiveAction.performed+= onPerspective;
_moveAction.canceled += onMove;
_lookAction.canceled += onLook;
_sprintAction.canceled += onSprint;
_jumpAction.canceled += onJump;
_zoomAction.canceled += onZoom;
_perspectiveAction.canceled += onPerspective;
}
private void onMove(InputAction.CallbackContext context)
{
Move = context.ReadValue<Vector2>();
}
private void onLook(InputAction.CallbackContext context)
{
Look = context.ReadValue<Vector2>();
}
private void onSprint(InputAction.CallbackContext context)
{
Sprint = context.ReadValueAsButton();
}
private void onZoom(InputAction.CallbackContext context)
{
Zoom = context.ReadValue<Vector2>();
}
private void onJump(InputAction.CallbackContext context)
{
Jump = context.ReadValueAsButton();
}
private void onPerspective(InputAction.CallbackContext context)
{
}
private void OnEnable()
{
_currentMap.Enable();
}
private void OnDisable()
{
_currentMap.Disable();
}
}