How to shoot two bullet at the same time

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ordinary

Here My Code

  
func shoot():
	look_at(get_global_mouse_position())
	if Input.is_action_pressed("Attack") and can_fire:
		var bullet_instance = bullet.instance()
		bullet_instance.position = position 
		bullet_instance.rotation_degrees = -rotation_degrees
		bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed, 0).rotated(rotation))
		get_tree().get_root().add_child(bullet_instance)
		can_fire = false
		yield(get_tree().create_timer(fire_rate), "timeout")
		can_fire = true

Hi, Try this:

  var max_bullets:int= 2	
    func shoot():
    	look_at(get_global_mouse_position())
    	if Input.is_action_pressed("Attack") and can_fire:
    		for i in max_bullets:
    			var bullet_instance = bullet.instance()
    			bullet_instance.position = position + Vector2(i*10,0)
    			bullet_instance.rotation_degrees = -rotation_degrees
    			bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed, 0).rotated(rotation))
    			get_tree().get_root().add_child(bullet_instance)
    		can_fire = false
    		yield(get_tree().create_timer(fire_rate), "timeout")
    		can_fire = true

estebanmolca | 2020-05-30 10:46

:bust_in_silhouette: Reply From: deaton64

Hi,

You could do something like below to get both bullets at the some time. I’ve moved x over by 50. You don’t have to create a new var to hold the second bullet if you don’t need to (move add_child up and above the second instance line) and you could put it all in a function and pass position to where the bullet position should be.

func shoot():
    look_at(get_global_mouse_position())
    if Input.is_action_pressed("Attack") and can_fire:
    var bullet_instance = bullet.instance()
    bullet_instance.position = position 
    bullet_instance.rotation_degrees = -rotation_degrees
    bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed, 0).rotated(rotation))
    var bullet_instance2 = bullet.instance()
    bullet_instance2.position = position + Vector2(50, 0)
    bullet_instance2.rotation_degrees = -rotation_degrees
    bullet_instance2.apply_impulse(Vector2(),Vector2(bullet_speed, 0).rotated(rotation))
    get_tree().get_root().add_child(bullet_instance)
    get_tree().get_root().add_child(bullet_instance2)

    can_fire = false
    yield(get_tree().create_timer(fire_rate), "timeout")
    can_fire = true

Sorry I did not see that you had already commented, basically I put the same answer

estebanmolca | 2020-05-30 10:52

yeah I do that a lot :slight_smile:
I check nobody has replied, paste my answer and there’s a reply before mine.

deaton64 | 2020-05-30 11:02