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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Gamepad updated this frame

Discussion in 'Input System' started by MousePods, Dec 10, 2019.

  1. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    759
    Hi,

    I am printing: print(Gamepad.current.wasUpdatedThisFrame);

    It seems to work most of the time, however, if I leave the controller on the table without touching it, it will occasionally printer true even nothing is being pressed. Is this a known issue or am I misinterpreting the meaning of this?

    If this is the way it is suppose to work, is there a method to detect if the player hit any button on the gamepad so I can determine if the player is using the gamepad or keyboard?

    Thanks!
     
    Last edited: Dec 10, 2019
    grrava likes this.
  2. grrava

    grrava

    Joined:
    Nov 11, 2012
    Posts:
    37
    I had the exact same question, I use this property now, seems to work fine, but feedback is appreciated:
    Code (CSharp):
    1. private bool isPlayerUsingMouseInput = false;
    2. public bool IsPlayerUsingMouseInput
    3. {
    4.     get
    5.     {
    6.         if (isPlayerUsingMouseInput)
    7.             isPlayerUsingMouseInput = !Gamepad.current.wasUpdatedThisFrame;
    8.         else
    9.             isPlayerUsingMouseInput = Mouse.current.wasUpdatedThisFrame;
    10.         return isPlayerUsingMouseInput;
    11.     }
    12. }
     
    Last edited: Dec 20, 2019
  3. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    759
    Hi,

    Unfortunately, I cannot do that as my game is for mobile and the player won't be touching the screen constantly. :(

    Thanks for the snippet though!
     
  4. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    759
    Bump - It is still an issue in the latest update.
     
  5. Evyatron7

    Evyatron7

    Joined:
    Mar 6, 2019
    Posts:
    18
    I haven't used this personally - but maybe you'd want to use this instead?

    Code (CSharp):
    1. Gamepad.current.CheckStateIsAtDefaultIgnoringNoise
    I imagine what you're getting might be noise from the sticks.
     
  6. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    It's sort of expected behavior. wasUpdatedThisFrame only requires an event to be received. The event may, however, end up not containing any state changes or may have only noise.

    The most reliable way for this is to employ the help of InputUser.

    Code (CSharp):
    1. // Create a user and give it the keyboard and the mouse.
    2. var user = InputUser.PerformPairingWithDevice(Keyboard.current);
    3. InputUser.PerformPairingWithDevice(Mouse.current, user: user);
    4.  
    5. // Listen for any use of a device that isn't paired to a user.
    6. ++InputUser.listenForUnpairedDeviceActivity;
    7. InputUser.onUnpairedDeviceUsed +=
    8.     (control, eventPtr) =>
    9.     {
    10.         var device = control.device;
    11.         if (device is Gamepad)
    12.         {
    13.             // User interacted with a control on a gamepad not paired to the user.
    14.             // Let's restrict it to button pressed.
    15.             if (!(control is ButtonControl))
    16.                 return;
    17.  
    18.             user.UnpairDevices();
    19.             InputUser.PerformPairingWithDevice(device, user: user);
    20.         }
    21.     }
    ATM there's no bool getter equivalent to this detection.
     
    florianBrn and ZingZangGames like this.
  7. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    759
    Thank you for the information!
     
  8. Arokma

    Arokma

    Joined:
    Oct 14, 2015
    Posts:
    20
    Hi @Rene-Damm,
    is there now a better way of knowing if a Mouse of gamepad has been used ?

    Thanks in advance