Search Unity

GamePad Stick/Buttons as Mouse Move/Click UI

Discussion in 'Input System' started by PedroD6Peo, Oct 20, 2019.

  1. PedroD6Peo

    PedroD6Peo

    Joined:
    Apr 21, 2014
    Posts:
    3
    Hello,

    I try to do a system that switch between the Mouse and the Gamepad for UI control.
    I need : Left stick of the Gamepad control the Cursor and Gamepad button control the left Mouse click

    I Use :
    The new InputSystemUIInputModule.
    For the moment I move the cursor with this function : Mouse.current.WarpCursorPosition();
    I made my asset control with UI map actions. And I add gamepad binding for Click, Point and Move.

    But all events (over, pointerdown, etc) doesn't work with game pad.

    Someone have an idea to achieve this system with new InputSystem?

    Thanks a lot for your help
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Built-in support for this is on the list for after 1.0.

    For now, I'd recommend adding a virtual mouse and driving that. This will generally work better than trying to hijack the system mouse with WarpCursorPosition.

    Code (CSharp):
    1. var mouse = InputSystem.AddDevice<Mouse>();
    2.  
    3. // Somewhere in update.
    4. var mouseDelta = gamepadInputAction.ReadValue<Vector2>() * m_MouseSpeed;
    5. var currentPosition = mouse.position.ReadValue();
    6. InputSystem.QueueStateEvent(mouse,
    7.     new MouseState
    8.     {
    9.         position = currentPosition + delta,
    10.         delta = delta,
    11.         // Set other stuff like button states...
    12.     });
     
    Pnvanol likes this.
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Just for reference, 1.0.0-preview.5 will come with an example for how to set up a gamepad mouse cursor which also contains a reusable component that does most of the work.
     
    Pixelith likes this.
  4. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    580
    I was googling around for a way to control my cursor with my controller, and found this gem of a post buried in the results. Saved me a huge headache!
     
    John_Leorid likes this.
  5. peterdfinn

    peterdfinn

    Joined:
    Aug 15, 2018
    Posts:
    5
    This discussion was an absolute life saver! Thank you so much!
     
  6. AceOfSpades4654

    AceOfSpades4654

    Joined:
    Feb 19, 2021
    Posts:
    1
    I was able to understand whats happening here for the most part and I really like this is available. I'm trying to do something kind of similar which is to add input from an Oculus controller to a virtual gamepad. I am wondering what the new MouseState is, is that an API in Unity? Is there one similar to gamepads?
     
  7. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    122
    Installed that example, how do I switch the mouse on and off, so that I can run and fire with the gamepad for example. Any object i should disable?
    The example automatically switches to the action set of UI, but I can't seem to be able switch that off.
    Thank you
     
    imaginationrabbit likes this.
  8. imaginationrabbit

    imaginationrabbit

    Joined:
    Sep 23, 2013
    Posts:
    349
    I had the same problem- just do some checks and if you want to disable the VirtualMouseInput script disable it at the end of OnEnable- I wanted it to disable if there are no gamepads so I put this at the end of OnEnable on that script
    Code (CSharp):
    1.  if (Gamepad.all.Count == 0)
    2.             {
    3.                   this.enabled = false;
    4.             }
    5.             else
    6.             {
    7.                 Debug.Log(Gamepad.all.Count + " gamepad's detected");
    8.             }
     
    Pnvanol and Pixelith like this.
  9. X3doll

    X3doll

    Joined:
    Apr 15, 2020
    Posts:
    34
    I don't know if it would be helpful for a future me BUT

    [Version 1.4.4 - November 03, 2022]

    The example scene of the gamepad cursors is broken by mistake.

    To fix that you need to set the blocking mask of the graphic raycaster into the canvas to 2D ( In the example import blocking mask is NONE causing the example to not work )

    Then, a tip for a future me. Use the virtual cursor only for the gamepad cursor, please i see too many people make this error and use them together with the actual game mouse. They don't work well together.

    But you can use the controls scheme to actually detect the correct scheme to activate the gamepad virtual cursor. In this way, you can quickly swap virtual cursor component (Only for Gamepad schemes) and the real cursor (Cursor API/Custom Pointer Implementation) (Only for keyboard/mouse schemes)