0 votes

I have added a custom mapping in the godot project settings called "shoot" and mapped it to the spacebar. I have my spaceship movement working with the arrow keys, and shooting also works, but when I hold down certain combinations of the arrow keys and the space bar, what I would expect to happen is for the spaceship to continue moving in the desired direction, and shoot while it is moving. Instead, it freezes as long as I am holding the spacebar, and when I release the spacebar, it continues to move but does not shoot.

Here is the script for the spaceship. If the script for the bullet is necessary as well, I will be happy to send it.

extends KinematicBody2D


var velocity = Vector2.ZERO
const SPEED = 250

var bullet_scn = preload("res://Bullet.tscn")

func _physics_process(delta):
    var play_on = false

    if Input.is_action_pressed('ui_down'):
        velocity.y += SPEED
        play_on = true
    if Input.is_action_pressed('ui_up'):
        velocity.y -= SPEED
        play_on = true
    if Input.is_action_pressed('ui_right'):
        velocity.x += SPEED
        play_on = true
    if Input.is_action_pressed('ui_left'):
        velocity.x -= SPEED
        play_on = true
    if Input.is_action_just_pressed('shoot'):
        print("Shooting")
        var bullet = bullet_scn.instance()
        # bullet.position = get_global_position()
        get_parent().add_child(bullet)
        get_parent().move_child(bullet, 0)

    if play_on:
        $Sprite.play("on")
    else:
        $Sprite.play("idle")



    move_and_slide(velocity)
    velocity = Vector2.ZERO

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass


func _on_Asteroid_body_entered(body):
    pass
Godot version 3.2.3 stable
in Engine by (12 points)

2 Answers

–1 vote

I think your problem is that when holding two opposite directions at once they will cancel out, other than that I'm not sure.
Try

if Input.is_action_pressed('ui_down'):
    velocity.y += SPEED
    play_on = true
elif Input.is_action_pressed('ui_up'):
    velocity.y -= SPEED
    play_on = true
if Input.is_action_pressed('ui_right'):
    velocity.x += SPEED
    play_on = true
elif Input.is_action_pressed('ui_left'):

If you are having problems with being able to shoot you want to spawn your bullet on a Position2D that you should put in front of where you want to shoot from

try

if Input.is_action_just_pressed('shoot'):
    print("Shooting")
    var bullet = bullet_scn.instance()
    bullet.position = $Position2D.global_position
    get_parent().add_child(bullet)
    get_parent().move_child(bullet, 0)
by (126 points)
edited by
+1 vote

Apparently it depends on your keyboard how many keys can be pressed at once and which combinations can be pressed at the same time.

You can test this here: https://www.xem.us/rollover/rollover.html

Also, shoutouts to this https://www.reddit.com/r/godot/comments/8466za/unable_to_register_multiple_keypresses/ (thats where I found out)

by (16 points)

Wow, thanks for this answer and the link to the tester!
I totally did not know that!

(I am on a Mac and only 2 arrow keys are allowed at the same time, on WASD however it's no problem.)

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.