+1 vote

Hi,
For a 3D game, I need a virtual joystick for touchscreen devices, and I want use it for control the translation of a KinematicBody. I am using GDScript for this game.
How I can do it?
Thanks in advanced.

Godot version 3.3
in Engine by (352 points)

1 Answer

+2 votes
Best answer

You need a scene with a node that follows the touch position (a sprite maybe).
To simulate joy axes, get its position in the scene together with the width and height of the scene. You can use a Position2D node placed at the bottom right of the scene to find the height and width of the scene by calling $Position.get_position().

To get values ranging from -1 to 1 use:
-1 + (touch_x_position*2)/width and
-1 + (touch_y_position*2)/height (but remember +y is downwards so hopefully you will figure out how to flip the values)

All you need now is to listen for touch input events and get your virtual joystick axes values and use them like you use real joysticks.

I could write the whole script but that will take some time so I hope this is useful enough to give you an idea on how to go about it.

by (1,017 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.