0 votes

So I have a space ship. I want it to alternate firing between the left and right guns when I hold the mouse. I've messed with it a lot and have produced a few different behaviors but never the one I want. At one point I had it shooting and alternating correctly but only when I repeatedly clicked the LMB and not when I held it.

I started over and now I can tell the game knows when I am holding the button but there are different problems now. When I hold the LMB the bullet spawns and stays stuck to its spawn point until I release the button, then it flies as it should. However, when I fire another bullet, the old one disappears. I haven't got around to adding a timer yet, as I can't get the initial behavior I want yet. Some advice would be much appreciated. Keep in mind, I'm pretty much an absolute beginner!

[Here is a link to a recorded .gif that shows behavior.]
https://gyazo.com/8b7392e1a33e1449bbe7d08b2ad6b96a

and here is my code.

#Start of player "action" controller.
var Bullet = preload("res://Scenes/Bullet.tscn")
var alt = 0
var LMB_held = false
var bi = Bullet.instance()

func _input(event):
    if event is InputEventMouseButton:
        if event.is_pressed():
            set_process(true)
func _process(delta):
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        alt == 0
        bi.start($LeftGun.global_transform)
        get_parent().add_child(bi)
        alt+=1
    if alt == 1:
        bi.start($RightGun.global_transform)
        get_parent().add_child(bi)
        alt-=1

Ignore the LMB_Held variable. It isn't used right now, I was trying a few things and haven't deleted it yet.

asked Jul 27 in Projects by RebelNinja (12 points)

1 Answer

0 votes

Try something more like:

var Bullet = preload("res://Scenes/Bullet.tscn")
var alt = 0
var LMB_held = false
var bi = Bullet.instance()

func _input(event):
    if event is InputEventMouseButton:
        if event.is_pressed():
            set_process(true)

func _process(delta):
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        if alt == 0:
            bi.start($LeftGun.global_transform)
            get_parent().add_child(bi)
            alt = 1
        elif alt == 1:
            bi.start($RightGun.global_transform)
            get_parent().add_child(bi)
            alt = 0

The main thing is that you want to first check to see if you are pressing the left button, then you want to see which side to fire from. Each time, you want to set the alt variable so that the next time around, it will fire the other side. The way that you did it with alt += 1 or alt -= 1 should work with this updated code, but since you only ever want it to be 1 or 0, you can just set it to that directly.

answered Jul 28 by Eric Ellingson (578 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.