Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Connection of the driver's pedal to the Unity Input System

Discussion in 'Scripting' started by JaimeGranel, Oct 4, 2023.

  1. JaimeGranel

    JaimeGranel

    Joined:
    Nov 6, 2020
    Posts:
    15
    Hello, I am trying to connect a Thrustmaster pedal to Unity with the new input system, but I am encountering a few problems. Mainly, this pedal does not have a controller, so it appears in the Unity debug as an unsupported (HID). I solved this by manually creating a registry in the input system, making it pass through a gamepad with the following code extracted from the Unity documentation.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.InteropServices;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEngine.InputSystem;
    7. using UnityEngine.InputSystem.DualShock;
    8. using UnityEngine.InputSystem.Layouts;
    9. using UnityEngine.InputSystem.LowLevel;
    10. using UnityEngine.InputSystem.Utilities;
    11.  
    12. [InputControlLayout(stateType = typeof(MyDeviceReport))]
    13. #if UNITY_EDITOR
    14. [InitializeOnLoad]
    15. #endif
    16. public class MyDevice: InputDevice
    17. {
    18.     static MyDevice()
    19.     {
    20.         InputSystem.RegisterLayout<MyDevice>("PedalsSIM", new InputDeviceMatcher()
    21.             .WithInterface("HID")
    22.             .WithManufacturer("Thrustmaster")
    23.             .WithProduct("Sim Pedals"));
    24.  
    25.     }
    26.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    27.     static void Init() { }
    28. }
    29.  
    30. [StructLayout(LayoutKind.Explicit, Size = 32)]
    31. struct MyDeviceReport : IInputStateTypeInfo
    32. {
    33.     public FourCC format => new FourCC('H', 'I', 'D');
    34.  
    35.     [InputControl(name = "Throttle", layout = "Analog", format = "BYTE", offset = 2)]
    36.     [FieldOffset(2)] public byte Throttle;
    37.     [InputControl(name = "Clutch", layout = "Analog", format = "BYTE", offset = 4)]
    38.     [FieldOffset(4)] public byte Clutch;
    39.     [InputControl(name = "Brake", layout = "Analog", format = "BYTE", offset = 5)]
    40.     [FieldOffset(5)] public byte Brake;
    41. }
    42.  
    upload_2023-10-4_10-57-18.png

    With this, I have been able to visualize it in the debug input, but I am not able to obtain information from this through the action input.

    upload_2023-10-4_10-57-41.png

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class MyController : MonoBehaviour
    5. {
    6.     public NewControls InputMaster { get; private set; }
    7.     InputAction mAction1;
    8.     private void Awake()
    9.     {
    10.         InputMaster = new NewControls();
    11.         AddInputMasterEvents(InputMaster);
    12.     }
    13.     public void AddInputMasterEvents(NewControls input)
    14.     {
    15.         if (input != null)
    16.         {
    17.             mAction1 = input.pedals.a;
    18.  
    19.         }
    20.     }
    21.     private void Update()
    22.     {
    23.  
    24.         Debug.Log($"{mAction1 != null} { (mAction1 != null ? (mAction1.ReadValue<float>() * 1000) : 0)}   ");
    25.     }
    26.  
    27. }
    How could I connect this device correctly? It's worth noting that it also appears in the Windows device manager, although it appears in the category of other devices, and even in the game manager it also appears and even I can see the values with which I press the pedals.

    upload_2023-10-4_11-1-12.png
    upload_2023-10-4_11-1-24.png
     
  2. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326