0 votes

I am a beginner and been watching tutorials in YouTube for a 2D Space Shooter game. I plan to develop one for mobile phones. However, all videos I have seen used the keyboard for movement, so I searched for one creating a virtual joystick. I have seen one and successfully integrated it with the code. So now, I have a virtual joystick and a fire button (TouchScreenButton) which are both child nodes of a canvas layer. Since I cannot test both at the same time because there is only 1 mouse pointer, I have included a spacebar to also call the fire/attack functionality.

During testing in the Godot Engine and Android Virtual Device, there seems to have no issues at all, both buttons are working well separately. However, when I tested it in an actual mobile phone, whenever the player was moving using the joystick and then I press the fire button, the player will stop moving. It seems they cannot function at the same time. But when I hold down the fire button for continuous firing, then I move the player with the joystick it can move.

Here is the script for the joystick (Joystick.gd)

extends Area2D

onready var bigCircle = $BigCircle
onready var smallCircle = $BigCircle/SmallCircle

onready var max_distance = $CollisionShape2D.shape.radius

var touched = false

func _input(event):
    if event is InputEventScreenTouch:
        var distance = event.position.distance_to(bigCircle.global_position)
        if not touched:
            if distance < max_distance:
                touched = true
        else:
            smallCircle.position = Vector2.ZERO
            touched = false

func _process(_delta):
    if touched:
        smallCircle.global_position = get_global_mouse_position()
        #clamp small circle
        smallCircle.position = bigCircle.position + (smallCircle.position - bigCircle.position).limit_length(max_distance)

func get_velocity():
    var joystickVelocity = Vector2.ZERO
    joystickVelocity.x = smallCircle.position.x / max_distance
    joystickVelocity.y = smallCircle.position.y / max_distance
    return joystickVelocity

Here is the code snippet in the player scene where the joystick and fire button (named Attack) functions can be called:

func _physics_process(delta):
    velocity = $UI/Joystick.get_velocity()
    velocity = velocity.normalized() * speed

    position += velocity * delta

    # make sure sprite within screen, adjusted to size of image
    var viewRect := get_viewport_rect()
    position.x = clamp(position.x, 23, viewRect.size.x - 23)
    position.y = clamp(position.y, 23, viewRect.size.y - 23)

    # used for testing using spacebar for Fire
    if Input.is_action_pressed("fire") and fireDelayTimer.is_stopped():
        fireDelayTimer.start(fireDelay)
        shoot.play()
        for child in firingPositions.get_children():
            var bullet := plBullet.instance()
            bullet.global_position = child.global_position
            get_tree().current_scene.add_child(bullet)

func _on_Attack_pressed():
    if fireDelayTimer.is_stopped():
        fireDelayTimer.start(fireDelay)
        shoot.play()
        for child in firingPositions.get_children():
            var bullet := plBullet.instance()
            bullet.global_position = child.global_position
            get_tree().current_scene.add_child(bullet)

Basically, how can I implement where the player can continuously move while firing in a mobile phone (multitouch)

Godot version 3.5
in Engine by (12 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.