Search Unity

Input System Release only

Discussion in 'Input System' started by Venividiviciuus, Jun 9, 2022.

  1. Venividiviciuus

    Venividiviciuus

    Joined:
    Nov 20, 2019
    Posts:
    13

    Hi everyone I'm trying to use the input system with a button to get a typical GetButtonDown so I set an interaction to Release only but it is never called using
    _switchCameraAction.performed + = onSwitchCamera;

    neither with started nor with canceled.
    in case I remove the interaction I get 2 calls every time it is executed but I need this to happen only once.
    What is the problem I don't understand .. "how I use Release only to get a single button call on release"

    https://ibb.co/q1z52nq
     
  2. coatline

    coatline

    Joined:
    Jul 31, 2019
    Posts:
    17
    You could remove the interaction and do something like this:

    Code (CSharp):
    1. public void OnButtonPressed(InputAction.CallbackContext value)
    2. {
    3.         if(value.performed)
    4.             // Pressed Down
    5.         else if(value.canceled)
    6.             // Let up
    7. }
    I'm sure there is a better way but this is how I do it. :)
     
  3. Venividiviciuus

    Venividiviciuus

    Joined:
    Nov 20, 2019
    Posts:
    13
    @coatline When I press my function it is called more than once I don't understand why... this happens with a single press-release https://ibb.co/h1Zt8vR
    indeed the action of the button continues indefinitely
    I'll post my codes

    This is my InputManager:

    Code (CSharp):
    1. public class InputManager : MonoBehaviour
    2. {
    3.     public bool SwitchCamera { get; private set; }
    4.  
    5.     private InputActionMap _currentMap;
    6.  
    7.     private InputAction _switchCameraAction;
    8.  
    9.  
    10.     private void Awake()
    11.     {
    12.         _switchCameraAction = _currentMap.FindAction("SwitchCamera");
    13.  
    14.         _switchCameraAction.performed += onSwitchCamera;
    15.  
    16.         _switchCameraAction.canceled += onSwitchCamera;
    17.     }
    18.  
    19.  
    20.     private void onSwitchCamera(InputAction.CallbackContext context)
    21.     {
    22.         if (context.performed)
    23.             SwitchCamera = context.ReadValueAsButton();
    24.         else if (context.canceled) { }
    25.     }
    26. }

    This is my Function for SwitchCamera

    Code (CSharp):
    1. private void HandleCamera()
    2.     {
    3.         if (!_inputManager.SwitchCamera) return;
    4.  
    5.         if(_inputManager.SwitchCamera)
    6.         {
    7.             _camera = !_camera;
    8.         }
     
    Last edited: Jun 9, 2022
  4. Venividiviciuus

    Venividiviciuus

    Joined:
    Nov 20, 2019
    Posts:
    13
    I tried this method and i reported that the started is not called; while performed is called twice the first on Button Pressed and the second time on Button Release; instead, the canceled is called one time only in the same way of Button Release. For this I called my function using this process. But as a result ... the function is not performed.

    Code (CSharp):
    1.         if (context.started) Debug.Log("started");
    2.         else if (context.performed) Debug.Log("performed");
    3.  
    4.         else if (context.canceled)
    5.         {
    6.             SwitchCamera = context.ReadValueAsButton();
    7.             Debug.Log("canceled");
    8.         }
     
  5. Venividiviciuus

    Venividiviciuus

    Joined:
    Nov 20, 2019
    Posts:
    13
    The problem is that the function it will be called at each frame and the only way is to add a bool to check if the switch is possible but in my case it is extremely impossible to have this check. as unlike a jump input which can occur if the character is grounded until it is back on the ground then the input system will only execute, how can I make a camera switch (FPS to TPS) with this system?
     
  6. Venividiviciuus

    Venividiviciuus

    Joined:
    Nov 20, 2019
    Posts:
    13
    Solved

    Code (CSharp):
    1.  
    2.     public bool SwitchCamera { get; set; }
    3.  
    4.  
    5.     private void Awake()
    6.     {
    7.         _switchCameraAction = _currentMap.FindAction("SwitchCamera");
    8.         _switchCameraAction.performed += onSwitchCamera;
    9.     }
    10.  
    11.     private void onSwitchCamera(InputAction.CallbackContext context)
    12.     {
    13.         SwitchCamera = !SwitchCamera;
    14.     }
    Code (CSharp):
    1. private void HandleCamera()
    2. {
    3.         if (!_inputManager.SwitchCamera) return;
    4.  
    5.         _camera = !_camera;
    6.         _inputManager.SwitchCamera = !_inputManager.SwitchCamera;
    7. }
     
    coatline likes this.