Search Unity

KISS

Discussion in 'Input System' started by freekstorm, Jun 5, 2017.

  1. freekstorm

    freekstorm

    Joined:
    Nov 11, 2010
    Posts:
    86
    Using the acronym KISS, why not simply allow us to get the inputs by device.

    Controller theController=Input.controller[3];// get the third controller
    string name=theController.name;
    bool pressed=theController.button[1].pressed;
    bool changed=theController.button[1].changed;
    float axis=theController.axis[0];

    You can stick any more complicated stuff over the top of that for users who cant program, but this is what I need.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Sure, you'll be able to do that.

    The input system handles all input devices, so including mouse and keyboard etc.

    You can do
    InputDevice device = InputSystem.devices[0];​
    though you don't know what type of device you get.

    Or you can do
    Gamepad gamepad = InputSystem.LookupDevice(typeof(Gamepad), 0);​
    to get the first gamepad. Or just this if you want the most recently used one:
    Gamepad gamepad = Gamepad.current;​

    Then you can do things like
    float axis = ((AxisControl)device.controls[0]).value;​
    and
    bool wasJustPressed = ((ButtonControl)device.controls[1]).wasJustPressed;​

    Or for a more specific type of device, like gamepads:
    float axis = gamepad.leftStickX.value;​
    and
    bool wasJustPressed = gamepad.rightTrigger.wasJustPressed;​
     
    Peter77 and PhilSA like this.
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,316
    How do you replace this gloubi boulga in the new iniput system?
    (it does buttontDown without need of an InputX monob sitting in your scene by tracking querry time)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InputX
    6. {
    7.     static bool fireDown, firePressed;
    8.     static float timeLastFireDownQuerry, timeLastFireQuerry;
    9.  
    10.     public static bool IsFireButtonDown ()
    11.     {
    12.         if (Time.time == timeLastFireDownQuerry)
    13.         {
    14.             return fireDown;
    15.         }
    16.         else
    17.         {
    18.             timeLastFireDownQuerry = Time.time;
    19.             if (fireDown)
    20.             {
    21.                 fireDown = false;
    22.             }
    23.             else if (Input.GetButton ("Fire") || Input.GetAxis ("Fire") > .1f)
    24.             {
    25.                 fireDown = true;
    26.             }
    27.             return fireDown;
    28.         }
    29.     }
    30.  
    31.     public static bool IsFireButtonPressed ()
    32.     {
    33.         IsFireButtonDown ();
    34.         if (Time.time == timeLastFireQuerry)
    35.         {
    36.             return firePressed;
    37.         }
    38.         else
    39.         {
    40.             timeLastFireQuerry = Time.time;
    41.             firePressed = Input.GetButton ("Fire") || Input.GetAxis ("Fire") > .1f;
    42.             return firePressed;
    43.         }
    44.     }
    45. }
    46.