Godot Version
extends CharacterBody2D
var SPEED = 50
var player
var chase
func _ready():
get_node("AnimatedSprite2D_enemy").play("idle")
func _physics_process(delta):
if chase == true:
if get_node("AnimatedSprite2D_enemy").animation != "death":
get_node("AnimatedSprite2D_enemy").play("run")
player = get_node("../../Player/Player")
#var direction = (player.global_position - self.position).normalized()
var direction = (player.global_position - self.global_position).normalized()
if direction.x > 0:
get_node("AnimatedSprite2D_enemy").flip_h = true
else:
get_node("AnimatedSprite2D_enemy").flip_h = false
velocity.x = direction.x * SPEED
else:
if get_node("AnimatedSprite2D_enemy").animation != "death":
get_node("AnimatedSprite2D_enemy").play("idle")
velocity.x = 0
move_and_slide()
func _on_detection_area_body_entered(body):
if body.name == "Player":
chase = true
func _on_detection_area_body_exited(body):
if body.name == "Player":
chase = false
func _on_player_collision_body_entered(body):
if body.name == "Player":
Game.playerHP -= 1
func _on_caminy_death_body_entered(body):
if body.name == "Player":
death()
func death():
Game.gold += 5
Utils.saveGame()
chase = false
get_node("AnimatedSprite2D_enemy").play("death")
await get_node("AnimatedSprite2D_enemy").animation_finished
self.queue_free()
Question
If I run into the enemy the player is hurt but if I wait for the enemy to run to and collide with the player, the damage isn’t dealt
checking for a collision from both sides doesn’t solve this problem and also causes double the damage to be dealt (logically)
How can I fix this?
I thought about maybe just putting a bool on the collision of both sides and if it’s true (checked from global script), the damage is dealt… but this solution seems to be way to unnecessarily complicated for that task, isn’t it?