My older brother is making games he wants to add controller compatibility to. How to make cursor movable by joysticks?
Do you literally mean move the cursor? If you are on the old Input-Manager and on Windows , you can do so like this:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
int xPos = 0;
int yPos = 0;
SetCursorPos(xPos, yPos);
If you just meant move your player rather than the cursor, you can use controllers with the old system quite easily. Edit
→ Project Settings
→ Input Manager
and check what is set for controllers. On my system, the PS5 controller works with no additional settings.
If you’re on a macOS or a Console, you’ll have to change to the new Input-System and do what @dk404 says above, though using the New Input-System is a big learning curve there are some limitations (for example OnMouseDown
/OnMouseUp
are not supported, which means rewriting code if you use those events).
With the new Input System, that you can install with the Package Manager, you can use the gamepad to drive the mouse (see the sample Gamepad Mouse Cursor using a virtual cursor) or move (“warp”) the system mouse cursor depending on the Joystick input values.
There is also a paid solution in the unity asset store that does this for Windows only: