0 votes

I made a virtual joystick. With the help of the first virtual joystick, I move and jump. With the help of the second, I have to look in different directions. But I do not know how to do a rotation with second virtual joystick. My game for android

## Joystick Scipt ##

extends TouchScreenButton

var radius = Vector2(28, 28)
var boundary = 64

var ongoing_drag = -1

var return_accel = 20

var threshold = 10

func process(delta):
if ongoing
drag == -1:
var posdifference = (Vector2(0, 0) - radius) - position
position += pos
difference * return_accel * delta

func getbuttonpos():
return position + radius

func input(event):
if event is InputEventScreenDrag or (event is InputEventScreenTouch and event.is
pressed()):
var eventdistfromcentre = (event.position - getparent().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 getvalue():
if get
buttonpos().length() > threshold:
return get
button_pos().normalized()
return Vector2(0, 0)

Player Script

extends KinematicBody2D

onready var joystick1 = getparent().getnode("joystick/joystick1/joystickbutton")
onready var joystick2 = get
parent().getnode("joystick/joystick2/joystickbutton")

const FLOOR = Vector2(0, -1)
const GRAVITY = 970
const JUMPPOWER = 700
const MOVE
SPEED = 200

var vel = Vector2()

var target_dir = Vector2(0, 0)
var direction = Vector2(0, 0)

var DEADZONE = 0.4

func _procces():
pass

func physicsprocess(_delta):

target_dir = joystick1.get_value()

#WALK 
if joystick1.get_button_pos().length() > joystick1.threshold:
    vel.x = target_dir.x * MOVE_SPEED
    if $RayCast2D.is_colliding():
        $AnimatedSprite.play("walk")

else:
    vel.x = 0
    if $RayCast2D.is_colliding():
        $AnimatedSprite.play("idle")

# JUMP
if joystick1.get_button_pos().length() > joystick1.threshold:
    if is_on_floor():
        vel.y = target_dir.y * JUMP_POWER
    if not $RayCast2D.is_colliding():
        $AnimatedSprite.play("jump")

vel.y += GRAVITY * _delta

direction = joystick2.get_value()


vel = move_and_slide(vel, FLOOR);
asked Jun 28 in Projects by Konrad (14 points)

1 Answer

0 votes

One option I can think of is to find the angle of the touch position to the center of the joystick and apply it to your body.

var r = joystick_center.angle_to_point(event.position)
body.rotation = r

Something like the that.

You can also get a direction to move in like:

var dir = Vector2.RIGHT.rotated(r)
body.position += dir
answered Jun 29 by indicainkwell (108 points)
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 How to use this Q&A? before posting your first questions.