This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes

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 (353 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 (2,018 points)
selected by

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

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.

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

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 Frequently asked questions and 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 [email protected] with your username.