Set cursor position via gamepad in code

Hello there.

I’ve been making a script that listens to input from the right stick of the gamepad in order to indicate the direction in which the character will shoot within a radius from the center of the screen.

private const float RADIUS_MAX_MULTIPLIER = 0.85f;

...

[UsedImplicitly]
public void SetDirection(Vector2 direction) {
   var halfWidth = Screen.width / 2;
   var halfHeight = Screen.height / 2;
     
   var centerPosition = new Vector2Int(halfWidth, halfHeight);
     
   var radius = Math.Min(halfWidth, halfHeight) * RADIUS_MAX_MULTIPLIER;
     
   Mouse.current.WarpCursorPosition(centerPosition + direction * radius);
}

The character has a separate script that looks at the mouse position in Update() in order to direct the weapon in the direction of the cursor.

I also have a small script that looks at the last input device to change the icons on the hotkey tooltips.

My problem is that calling the last line triggers the mouse as the last input device. I downloaded the “Gamepad Mouse Cursor” sample in the package manager to check how the cursor movement was done for the gamepad, but it is not what I need.

Is there any way to indicate in the code that the cursor position change was caused by the Gamepad device?

My problem is that calling the last line triggers the mouse as the last input device.

From what you mentioned, this should be expected in case you’re calling Mouse.current.WarpCursorPosition, since this would be indirectly controlling the Mouse.current.position.

Seems you’re relying on the Mouse device to direct the direction of the weapon and this could probably be improved. Do you really need to warp the cursor?

You probably can abstract this behavior with something similar to the “Look” action (see the Look action in Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/DefaultInputActions.inputactions)

Yes, I made two separate actions, one for mouse, one for gamepad, both triggering “performed” event. Now it works as I wanted.

Although it works in this scenario, I’m afraid there might be a moment when I want the “dot” moving around the center to be able to interact with UI elements, and at the same time show the last used device correctly.

Maybe this can be done with VirtualMouse. I’ve seen a line like this:

InputState.Change(m_VirtualMouse.position, position);

However, I’m not sure which device will trigger the position change.