Make a touchscreen virtual joystick for 3D:

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

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.

:bust_in_silhouette: Reply From: magicalogic

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.

And how would you convert the vector2 to vector3 to move the player?

umma | 2023-01-16 14:38

You use the y axis of the virtual joystick to control movement along the z axis(forward and backwards) and the x axis of the virtual joystick to control the movement in along the x axis(sideways). They should work just like a real joystick works.

magicalogic | 2023-01-16 16:16

Sorry I was asking How would i implement that using transform.basis and move_and_slidebut I figured it out, thanks for replying.

umma | 2023-01-16 16:56