- Edited
Hello all,
I am pretty new to godot and have been trying to code my first game. However, I have run into a problem with my enemy scene. The AnimatedSprite has 2 animations: "walk" and "idle". I have created functions for the enemy to follow the player when within a certain range of the player, and within these functions added a command to change the animation state to the respective animation. However, when I first start the scene, the enemy always defaults to the walk animation. If the play trips it's follow mechanic, and then goes outside of its range, it goes back to the idle animation and everything works as it should. I have tried several solutions and I cannot get it to work. If anyone knows how to fix this, please let me know!
I have attached my code and node tree for help.
extends KinematicBody2D
export (int) var health := 10
onready var _animated_sprite = $AnimatedSprite
var speed = 50
var motion = Vector2.ZERO
var player = null
func _physics_process(delta):
motion = Vector2.ZERO
if player:
motion = position.direction_to(player.position) * speed
motion = move_and_slide(motion)
func handle_hit(damage: int):
health -= damage
if health <= 0:
queue_free()
func _on_Area2D_body_entered(body):
player = body
_animated_sprite.play("walk")
func _on_Area2D_body_exited(body):
player = null
_animated_sprite.play("idle")
func _on_Hitbox_body_entered(body):
if "Sword" in body.name:
Global.score += 1
queue_free()