I want my bullet to shoot only when there is no another bullet on the screen.

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

I was many expermenting with is_on_screen but does’t get anything can you help me?

this players code:

extends KinematicBody2D

var speed = 100 # speed in pixels/sec
var velocity = Vector2.ZERO
export (PackedScene) var Bullet

onready var muzzle = $Muzzle
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree

func get_input():
velocity = Vector2.ZERO
if Input.is_action_pressed(‘ui_right’):
velocity.x += 1
muzzle.rotation_degrees = 0
elif Input.is_action_pressed(‘ui_left’):
velocity.x -= 1
muzzle.rotation_degrees = -180
elif Input.is_action_pressed(‘ui_down’):
velocity.y += 1
muzzle.rotation_degrees = -270
elif Input.is_action_pressed(‘ui_up’):
velocity.y -= 1
muzzle.rotation_degrees = -90
if Input.is_action_just_pressed(‘shoot’):
shoot()

    velocity = velocity.normalized() * speed

func shoot():
var b = Bullet.instance()
owner.add_child(b)
b.transform = $Muzzle.global_transform

func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
if velocity != Vector2.ZERO:
animationTree.set(“parameters/Idle/blend_position”, velocity)

this bullet code

extends Area2D

var speed = 150

func _physics_process(delta):
position += transform.x * speed * delta

func _on_Bullet_body_entered(body):
if body.is_in_group(“mobs”):
body.queue_free()
queue_free()

:bust_in_silhouette: Reply From: betauer

The easiest option is creating a big Area2D as a child of your player with the size where you want to avoid a second bullet. It could be the screen size or any other. As soon as the bullet exit this area2d (you can listen for the area_entered signal because you bullet is also an Area2D) you can allow to the player a shot another one.

I have a position 2d on my tank so i don’t know how to make area 2d and to make my bullet be only one, and if you can show how to make this in my script i will be thankful for you.

Neloshok | 2021-02-13 04:11

Create the Area2D as a child of your tank in the editor. Doing in this way the Area2D position depends on its parent, the tank

betauer | 2021-02-15 19:25

Ok, I get it.

Neloshok | 2021-02-16 02:14