- Edited
Greetings y'all.
I'm rather new to playing with Godot (loving it!) and I have what should be a simple problem.
I am implementing a virtual joystick for a mobile game. Think Survivor.io
I have the input working, and I have the joystick node as a child to the camera. Everything was working fine before I added the camera, and everything continued to be fine when I made the game world infinite. However, upon limiting horizontal scrolling, the joystick GUI now drifts to the left and right along with the player.
The result of the input is still based on where the user touches the screen (acting as the new origin for the joystick) but the graphic for the joystick now drifts left or right. My goal is to lock the origin location upon touch and keep its position relative to the screen and not the camera.
I'll try to include as much relevant information as I can.
extends CanvasItem
signal use_move_vector
var pressed = false
var drag = false
var drag_dist_prev = 0
var drag_dist = 0
var drag_cd = 0
var drag_dur = 5
var drag_limit = 100
var drag_origin = Vector2(0,0)
var drag_position = Vector2(0,0)
var outer_origin = Vector2(0,0)
var inner_origin = Vector2(0,0)
var move_vector = Vector2(0,0)
var roll_vector = Vector2(0,0)
func _input(event):
if event is InputEventScreenTouch:
if event.pressed == false:
pressed = false
drag = false
move_vector = Vector2(0,0)
if drag_dist >= drag_limit:
roll_vector = drag_position - drag_origin
if event.pressed == true:
outer_origin = event.position
inner_origin = outer_origin
drag_origin = outer_origin
drag_position = drag_origin
drag_dist = 0
pressed = true
if event is InputEventScreenDrag:
if !drag:
drag = true
drag_cd = 0
drag_position = event.position
inner_origin = outer_origin - (outer_origin - event.position).limit_length(200)
move_vector = inner_origin - outer_origin
update()
func _draw():
var outer_rad = 200
var inner_rad = 100
var color = Color(1,1,1,0.5)
if pressed:
draw_circle(outer_origin, outer_rad, color)
if drag:
draw_circle(inner_origin, inner_rad, color)
func _ready():
pass
func _process(delta):
emit_signal("use_move_vector", move_vector.normalized()*delta, roll_vector.normalized()*delta)
roll_vector = Vector2(0,0)
if pressed:
drag_dist_prev = drag_dist
drag_dist = drag_origin.distance_to(drag_position)
if drag_dist == drag_dist_prev or drag_cd == 0:
drag_origin = drag_position
drag_cd = (drag_cd + 1) % drag_dur