Search Unity

Question Custom Device: Flip Initial Value from 1 to 0

Discussion in 'Input System' started by Deleted User, Nov 14, 2020.

  1. Deleted User

    Deleted User

    Guest

    I am writing a custom device to add support for my DualShock 3 controller on Windows. I've managed to figure out most of it, apart from the two triggers.
    Code (csharp):
    1. [InputControl(name = "leftTrigger", format = "BYTE")]
    2. [FieldOffset(11)] public byte leftTrigger;
    3. [InputControl(name = "rightTrigger", format = "BYTE")]
    4. [FieldOffset(12)] public byte rightTrigger;
    These are the right offsets but, in the Input Debugger, they start with a value of 1 and change to 0 when fully pressed. How do I change these to start at 0 and move to 1 when fully pressed?

    I've looked at Processors but, apart from writing my own, the closest I can get is `Invert` which starts at -1 and changes to 0 when fully pressed.

    Thanks!
     
    Last edited by a moderator: Nov 14, 2020
  2. Deleted User

    Deleted User

    Guest

    My friend suggested, at least temporarily, to use a custom processor that flips the value based on a defined maxValue.

    I imagined there might be a built-in solution I was missing but, for now, this works:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.     [InitializeOnLoad]
    3. #endif
    4.     public class FlipProcessor : InputProcessor<float>
    5.     {
    6. #if UNITY_EDITOR
    7.         static FlipProcessor()
    8.         {
    9.             Initialize();
    10.         }
    11. #endif
    12.  
    13.         [RuntimeInitializeOnLoadMethod]
    14.         static void Initialize()
    15.         {
    16.             InputSystem.RegisterProcessor<FlipProcessor>();
    17.         }
    18.  
    19.         [Tooltip("TODO - Tooltip required")]
    20.         public float maxValue = 1;
    21.  
    22.         public override float Process(float value, InputControl control)
    23.         {
    24.             return this.maxValue - value;
    25.         }
    26.     }
    Code (CSharp):
    1. [InputControl(name = "leftTrigger", format = "BYTE", processors = "flip(maxValue=1)")]
    2. [FieldOffset(11)] public byte leftTrigger;
    3. [InputControl(name = "rightTrigger", format = "BYTE", processors = "flip(maxValue=1)")]
    4. [FieldOffset(12)] public byte rightTrigger;
     
    Last edited by a moderator: Nov 18, 2020