I’m making a 2D game and I have a problem with the particle system (or I wrote the code wrong).
For example, a bullet collided with a metal object (in my case, a car or a tank), upon impact, the bullet disappears, and sparks (particles) are created in its place, everything is fine, but when the bullet collides with a body (in my case, a soldier), then sparks appear anyway, although this should not be the case. Could you please tell me what I am doing wrong? I will be very grateful.
Code for the bullet:
extends BulletsParent
@onready var particles_to_car_tank = preload("res://Efects/solder_damage.tscn")
func _ready():
Signals.connect("bullet_exited_tree", Callable(self, "bullet_exit_into_scene"))
_set_direction(Vector2.LEFT)
func _on_body_entered(body):
if body is CarPlayer or Tank1 or Tank2:
var part_technick = particles_to_car_tank.instantiate()
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
part_technick.global_position = position
part_technick.global_rotation = 0
get_parent().add_child(part_technick)
queue_free()
if body is SolderPistol or SolderAutomat or SolderShotgun or SolderCommander:
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
queue_free()
To get started, first check which conditions apply:
func _on_body_entered(body):
if body is CarPlayer or Tank1 or Tank2:
print("Car or Tank")
var part_technick = particles_to_car_tank.instantiate()
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
part_technick.global_position = position
part_technick.global_rotation = 0
get_parent().add_child(part_technick)
queue_free()
elif body is SolderPistol or SolderAutomat or SolderShotgun or SolderCommander:
print("Solder")
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
queue_free()
The problem is that your function is called twice and each time has a different output, probably in the solder there is another object that is identified as a car or tank.
func _on_body_entered(body):
if body is SolderPistol or SolderAutomat or SolderShotgun or SolderCommander:
print("Solder")
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
queue_free()
elif body is CarPlayer or Tank1 or Tank2:
print("Car or Tank")
var part_technick = particles_to_car_tank.instantiate()
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
part_technick.global_position = position
part_technick.global_rotation = 0
get_parent().add_child(part_technick)
queue_free()
If you switch places, then when you collide with a tank or a car, no particles appear at all.
If a bullet hits a soldier, the particles are also not displayed, and only Solder is displayed in the console.
But if you put it as it was at first, then the console will display: Solder and Car or Tank.
Do you think the problem could still be a double function call?
I’m really confused, if and elif should never be executed at the same time!
Put this code, shoot once at the soldier and once at the tank or car and send the output
func _on_body_entered(body):
print("Enter")
if body is CarPlayer or Tank1 or Tank2:
print("Car or Tank")
var part_technick = particles_to_car_tank.instantiate()
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
part_technick.global_position = position
part_technick.global_rotation = 0
get_parent().add_child(part_technick)
queue_free()
elif body is SolderPistol or SolderAutomat or SolderShotgun or SolderCommander:
print("Solder")
Signals.emit_signal("bullet_damage_player", damage)
print("Enemy dmg to player: ", damage)
queue_free()
When shooting a soldier, the console displays:
Enter
Car or Tank
When a shot is fired at the car, it outputs:
Enter
Car or Tank
Although my equipment and soldiers have different collision masks, that’s why the bullet (when it collides with a tank or a car) cannot count that it collided with a soldier and vice versa.