Godot Version
Replace this line with your Godot version
Question
Hello!
I’m running into an issue in Godot where placing two of the exact same enemy scene into my level causes strange behavior (like them dying together, sharing health, or interfering with each other’s targeting).
The Issue: When there is only one enemy, it works fine. When I add a second one, they conflict.
Here is my enemy script:
extends CharacterBody2D
signal disable_collisons
signal disable_the_attack
var cool_down = false
var is_attacking = false
var dead = false
var is_punch = false
var health = 50
var SPEED = 300.0
var JUMP_VELOCITY = -400.0
var do_noises = true
var is_punching = false
var knockback_velocity: Vector2 = Vector2.ZERO
enum State { MOVE, LUNGE, KNOCKBACK }
var state = State.MOVE
var knockback = Vector2.ZERO
func _physics_process(delta: float) → void:
match state:
State.KNOCKBACK:
velocity.y += get_gravity().y \* delta \* 0.3
velocity.x = knockback.x
knockback.x = move_toward(knockback.x, 0, 800 \* delta)
if abs(knockback.x) < 10:
velocity.x = 0
state = State.MOVE
if knockback.length() < 10:
state = State.MOVE
State.LUNGE:
\# set lunge velocity here, then go back to MOVE when done
pass
move_and_slide()
if not is_on_floor():
velocity.y += get_gravity().y \* delta \* 0.3
if knockback.length() < 10:
velocity.x = 0
state = State.MOVE
if is_punch == false:
if is_punching == false:
if dead == false:
if is_attacking == false:
if velocity.x == 0:
$"nature taken knight shape".visible = true
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = false
$"green shoot other side".visible = false
$"AnimationPlayer of nature taken knight".play("nature taken knight idle animation")
$head.visible = false
$"left hand".visible = false
$"right hand".visible = false
$tourso.visible = false
if velocity.x != 0:
pass
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func _on_left_attack_get_ddemge() → void:
var dir = sign(global_position.x - $"../yafa".global_position.x)
knockback = Vector2(dir \* -300, 0)
state = State.KNOCKBACK
health -= 10
$"robot geting damged".play()
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = false
$"green shoot other side".visible = false
$head.visible = true
$"left hand".visible = true
$"right hand".visible = true
$tourso.visible = true
$"AnimationPlayer of nature taken knight".play("damged")
if health <= 0:
dead = true
print("dead")
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = false
$"green shoot other side".visible = false
$head.visible = true
$"left hand".visible = true
$"right hand".visible = true
$tourso.visible = true
$"AnimationPlayer of nature taken knight".play("deathy after")
disable_collisons.emit()
$"nature taken knight noises".stop()
await get_tree().create_timer(1).timeout
queue_free()
func _on_right_attack_get_damged() → void:
var dir = sign(global_position.x - $"../yafa".global_position.x)
knockback = Vector2(dir \* 300, 0)
state = State.KNOCKBACK
health -= 10
$"robot geting damged".play()
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$head.visible = true
$"left hand".visible = true
$"right hand".visible = true
$tourso.visible = true
$"AnimationPlayer of nature taken knight".play("damged")
if health <= 0:
dead = true
print("dead")
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$head.visible = true
$"left hand".visible = true
$"right hand".visible = true
$tourso.visible = true
$"AnimationPlayer of nature taken knight".play("deathy after")
disable_collisons.emit()
$"nature taken knight noises".stop()
await get_tree().create_timer(0.5).timeout
queue_free()
func _on_left_go_left() → void:
is_attacking = true
$head.visible = false
$"left hand".visible = false
$"right hand".visible = false
$tourso.visible = false
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = false
$"green shoot other side".visible = true
$"AnimationPlayer of nature taken knight".play("green shot animation lft other side")
$plant.play()
await get_tree().create_timer(0.3).timeout
is_attacking = false
func _on_right_play_right() → void:
is_attacking = true
$head.visible = false
$"left hand".visible = false
$"right hand".visible = false
$tourso.visible = false
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = true
$"green shoot other side".visible = false
$"AnimationPlayer of nature taken knight".play("green shhhot animaiton")
$plant.play()
await get_tree().create_timer(0.3).timeout
is_attacking = false
func _on_punch_right_punch_right() → void:
if is_punch == false:
is_punching = true
print("i am punching you!")
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = false
$"green shoot other side".visible = false
$head.visible = true
$"left hand".visible = true
$"right hand".visible = true
$tourso.visible = true
$head.flip_h = false
$"left hand".flip_h = false
$"right hand".flip_h = false
$tourso.flip_h = false
$"AnimationPlayer of nature taken knight".play("punch animation")
await $"AnimationPlayer of nature taken knight".animation_finished
is_punching = false
func _on_punch_left_punch_left() → void:
if is_punching == false:
is_punch = true
print("i am punching you!")
$"nature taken knight shape".visible = false
$"nature taken knight shape 2".visible = false
$"green shoooooooot".visible = false
$"green shoot other side".visible = false
$head.visible = true
$"left hand".visible = true
$"right hand".visible = true
$tourso.visible = true
$"AnimationPlayer of nature taken knight".play("punch anim")
await $"AnimationPlayer of nature taken knight".animation_finished
is_punch = false
func _on_lunch_right_lunch_right() → void:
if randf() < 0.5:
print(" i go -")
var dir = sign(global_position.x - $"../yafa".global_position.x)
knockback = Vector2(dir \* -400, 0)
state = State.KNOCKBACK
else:
print("i go ")
var dir = sign(global_position.x - $"../yafa".global_position.x)
knockback = Vector2(dir \* 400, 0)
state = State.KNOCKBACK
func _on_lunch_left_lunch_left() → void:
if randf() < 0.5:
print(" i go -")
var dir = sign(global_position.x - $"../yafa".global_position.x)
knockback = Vector2(dir \* -400, 0)
state = State.KNOCKBACK
else:
print(" i go ")
var dir = sign(global_position.x - $"../yafa".global_position.x)
knockback = Vector2(dir \* 400, 0)
state = State.KNOCKBACK