A simple virtual joystick for touchscreens, for both 2D and 3D games, with useful options.
A simple virtual joystick for touchscreens, with useful options.
GitHub Page: https://github.com/MarcoFazioRandom/Virtual-Joystick-Godot
Godot Engine: https://godotengine.org
Easy to use:
extends Sprite2D
@export var speed : float = 100
@export var joystick_left : VirtualJoystick
@export var joystick_right : VirtualJoystick
var move_vector := Vector2.ZERO
func _process(delta: float) -> void:
## Movement using the joystick output:
# if joystick_left and joystick_left.is_pressed:
# position += joystick_left.output * speed * delta
## Movement using Input functions:
move_vector = Vector2.ZERO
move_vector = Input.get_vector("ui_left","ui_right","ui_up","ui_down")
position += move_vector * speed * delta
# Rotation:
if joystick_right and joystick_right.is_pressed:
rotation = joystick_right.output.angle()
Joystick mode:
Dead zone size: If the tip is inside this range the output is zero.
Clamp zone size: The max distance the tip can reach.
Visibility mode:
Use input actions: if true the joystick will trigger the input actions created in Project -> Project Settings -> Input Map
In Godot, the input events from the mouse don't support multitouch, so make sure to have this configuration:
Project -> Project Settings -> General -> Input Devices
"emulate touch from mouse" ON
"emulate mouse from touch" OFF
⚠ This has been fixed in Godot Engine 4.2.1
Unfortunately, this a bug in the Godot engine, so the only solution for now is using Input.get_axis:
This doesn't work:
input_vector := Input.get_vector("ui_left","ui_right","ui_up","ui_down")
This works:
input_vector := Vector2.ZERO
input_vector.x = Input.get_axis("ui_left", "ui_right")
input_vector.y = Input.get_axis("ui_up", "ui_down")