Hi, i follow a tutorial and made a touch screen joystick controller for my topdown game,
this is the script for joystick: (attact to joystick node)
extends TouchScreenButton
var radius = Vector2(14,14)
var boundary = 27
var ongoing_drag = -1
var return_accel = 20
var threshold = 10
func _process(delta):
if ongoing_drag == -1:
var pos_diffrence = (Vector2(0,0) - radius) - position
position += pos_diffrence * return_accel * delta
func get_button_pos():
return position + radius
func _input(event):
if event is InputEventScreenDrag or (event is InputEventScreenTouch and event.is_pressed()):
var event_dis_from_centre = (event.position - get_parent().global_position).length()
if event_dis_from_centre <= boundary * global_scale.x or event.get_index() == ongoing_drag:
set_global_position(event.position - radius * global_scale)
if get_button_pos().length() > boundary:
set_position(get_button_pos().normalized() * boundary - radius)
ongoing_drag = event.get_index()
if event is InputEventScreenTouch and !event.is_pressed() and event.get_index() == ongoing_drag:
ongoing_drag = -1
func get_value():
if get_button_pos().length() > threshold:
return get_button_pos().normalized()
return (Vector2(0,0))
and this is the use for the player: (attact to player node)
var vel = Vector3()
onready var joistic = get_parent().get_node("joystic/joystic_button")
func _physics_process(delta):
var target_dir = Vector2(0,0)
target_dir = joistic.get_value()
this is my player controller:
target_dir = target_dir.normalized()
vel.x = lerp(vel.x, target_dir.x * speed , accel * delta)
vel.z = lerp(vel.z, target_dir.y * speed , accel * delta)
move_and_slide(vel, Vector3(0,1,0))
if i did not get it wrong, at the joystick script, position of button get stored the vector and
at the player script, its get called with get_value() function. And with target_dir = joistic.get_value()
this, player moves around as joystick button moves,
and what i try to do is, face the player object where it moves with joystick. long story short rotate it.