0 votes

I have a kinematic body and I want to move it using a joystick. I have followed Gonkee 's tutorial on how to make a virtual joystick but i am not able to implement it in my 3d project. I just want to have a sphere which rolls around the map, but twist is , we have to move the joy button to opposite side to move in a direction. I have done this much but didn't help:

for player :

extends KinematicBody

var acceleration = 10
var topspeed = 40
var joystick = get_parent().get_node("Joystick/joystick_button")
var vel = Vector3()
var speed = 30

func _ready():
    pass 

func _physics_process(delta):
    var target_dir = Vector2(0, 0)
    target_dir = joystick.get_value

    vel.x = lerp(vel.x, target_dir.x * speed , acceleration * delta)
    vel.z = lerp(vel.z, target_dir.z * speed , acceleration * delta)

    vel = move_and_slide(vel, Vector3.UP)

for joystick :

extends TouchScreenButton

var radius = Vector2(16, 16)
var boundary = 32
var ongoing_drag = -1 #-1 represents false and other integers represents different cases of true
var return_accel = 20
var threshold = 10

func get_button_pos():
    return position + radius

func _process(delta):
    if ongoing_drag == -1:
        var pos_diference = (Vector2(0, 0) - radius) - position
        position += pos_diference * return_accel * delta

func _input(event):
    if event is InputEventScreenDrag or (event is InputEventScreenTouch and event.is_pressed()):
        var event_dist_from_centre = (event.position  - get_parent().global_position).length()

        if event_dist_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)
in Engine by (44 points)

Please log in or register to answer this question.

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.