Search Unity

Triggering an InputAction?

Discussion in 'Input System' started by hungrybelome, Apr 21, 2021.

  1. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    Hi,

    I have a the c# code generated for my ActionMap, where I can get my input action reference. My input action is a simple click action. I have a custom input device that I want to use to trigger that input action. So when my custom input device says "click", I need to call something like "ClickAction.Trigger()", or the equivalent. Is there a way I can do this?

    Thanks
     
  2. babaqalex

    babaqalex

    Joined:
    Jan 28, 2016
    Posts:
    17
    I am trying to do the exactly the same thing. Have you figure it out?
     
  3. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    I gave up and ended up making a custom DeviceInput class, according to their docs, unfortunately.
     
  4. talha_munity

    talha_munity

    Joined:
    Nov 24, 2017
    Posts:
    2
    I had to do something similar before when the vive controllers wouldn't work with the new input system.

    The usual devices (KB/M, Joystick, Gamepad) were set to work with the new input system and the generated input actions. The vive controllers had their input read using SteamVR. I then added a layer on top of both of these to merge the input from all the devices. It was something like this:

    Code (CSharp):
    1.  
    2. // Wrapper class for handling input from ALL devices
    3. public class Player : MonoBehaviour
    4. {
    5.     public void Move3D(Vector2 pos)
    6.     {
    7.         // Respond to input
    8.     }
    9.     ... respond to other callbacks here ...
    10. }
    11.  
    Listen and respond to the Unity input system devices:

    Code (CSharp):
    1.  
    2. // Class for handling input from Unity Input System
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     public Player _player;
    6.  
    7.     public void OnMove3D(InputAction.CallbackContext context)
    8.     {
    9.         // Capture input from device and send to the wrapper class
    10.         var pos = context.ReadValue<Vector3>());
    11.         _player.Move3d(pos);
    12.     }
    13.     ... other callbacks here ...
    14. }
    15.  
    In another class, handle input from custom device:

    Code (CSharp):
    1.  
    2. // Class for handling input from custom device
    3. public class CustomDeviceInput: MonoBehaviour
    4. {
    5.     public Player _player;
    6.  
    7.     public void Move()
    8.     {
    9.         // Capture input from device and send to the wrapper class
    10.         var posX = device.ReadStickX();
    11.         var posY = device.ReadStickY();
    12.         Vector2 pos = new Vector2(posX, posY);
    13.         _player.Move3d(pos);
    14.     }
    15.     ... other callbacks here ...
    16. }
    17.  
    So basically the input from all devices is forwarded to the wrapper. The wrapper then acts upon the input with game logic. Any new device that needs to be added just needs to forward its input to this class.