How to use gyroaim when PS4 or Nintendo Switch gamepad is connected?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robotex

I want to use gyroaim like in FPS games at Nintendo Switch on consoles and mouse on PC for controlling aim point.

How to do it?

Godot handles accelerometer input differently than mouse input. Here is the docs section for retrieving a value from accelerometers. The docs are your friend.

DDoop | 2020-07-01 17:02

The docs clearly state that this method only works on iOS, Android and UWP, so this doesn’t answer the question. I’m also searching for this andhave foundo no way so far for godot to recognize the dual shock 4 gyro.

vandalk | 2021-04-10 13:50

:bust_in_silhouette: Reply From: humidifier

I know this is an old question, but thought I’d give some info that I found while trying to do the same. As far as I can tell, there is no way to retrieve gyro input from controllers in the godot engine itself, unless the gyro input is miraculously being read into one of the joy axes (which seems pretty unreliable).

To get around this, then, you need to implement reading data from external devices yourself, which is something that I think is impossible in GDScript. So there’s two options left, c++ or c#. From the two, c# is probably easier since godot natively supports it, though I think it wouldn’t be much harder using c++. The top answer from Accessing DualShock 4 motion sensor in Windows (ideally Unity) - Game Development Stack Exchange is a good guideline on what you should do, but I’ll go into more detail if you’re looking at only deploying to windows platforms.

You should grab code from the ds4windows github page, specifically the files in HidLibrary. In some script of your own, you should then call HidDevices.enumerate() and find the controller from these returned devices, say named gyrocontroller. You should then call gryocontroller.OpenDevice(false) to setup your controller to be read from. In _process, call gyrocontroller.ReadFile(inputReport), where inputReport is some 64 byte array defined in your script. inputReport will now be filled the most current input from your controller, which will contain gyro info. To find which bytes are relevant to gyro data is dependent on the type of controller and how it’s connected to your computer, but there are references online that cover that.