Manually moving mouse position / moving mouse position with joystick

Does anyone know how to assign mouse movement to a joystick controller (I realised I won’t be able to click GUI but I can use mouse contains + joystick button instead)?

Also does anyone know how to move the mouse cursor manually. Say I want it at point 100,100 on the screen, how do I make it go there?

I’ve done this in C++, so I know it’s possible. I think you’ll need to write a wrapper for the win32 function called SetCursorPos().

In the web-player you cant force move the mouse like that. Apparently you can in Windows if you include some .NET classes (you can search this in the forums, it has been discussed).

The solution to both your issues is to use a custom mouse cursor… You can use Screen.lockCursor = true to lock/hide the normal cursor and then use the mouse/joystick’s velocity to move your custom cursor around the screen. You can easily force your custom cursor to any arbitrary screen position as well. There may be a script already made on the wiki to do this.

It will not work though if you use OnGUI, as that relies on real mouse events and will only work if you really relay them through the OS

iirc, the equivalent method in osX is CGWarpMouseCursorPosition().

Turn your draw cursor off, then create a GUI cursor that reflects the position of the cursor position. Have it update to the mouse position so that when you use a mouse it looks right. When you use a joystick, have it move the position of the joystick then have it move the GUI cursor instead of the real cursor.

You could go as far to say:

cursorPosition+=(mouse movement this frame) + (joystick axis)

This would give you the updated cursor position with the mouse and joystick added.

I am not necessarily sure that you can affect the actual position of the cursor. It is available through the System.Windows.Forms.Cursor.Position, which if I am correct is not available through Unity.

I really like that last solution. I can just make it an option in the menu because my assumption is if they use the joystick they aren’t going to be using the mouse. I think I will try to implement that and see how it works.

I guess I would need to write my own mouse contains function for the joystick controller.

As mentioned above, if you want to use OnGUI and have it work correctly (including mouse hover etc) going through the native OS layer in a plugin is the only way.

If you use an own gui, using a virtual mouse pointer ie hiding the normal one and painting one yourself works pretty well :slight_smile: