Godot Version
4
Question
Godot-4, 2D
Hello, could you tell me how to properly animate the enemy in this code, I’ve been struggling with this error for an hour, I’ve tried a bunch of options, nothing works. I will be very grateful for the suggested answers!
extends CharacterBody2D
enum {
ATTACK,
IDLE,
WANDERING
}
var player: CharacterBody2D
const idlespeed = 50
const runspeed = 50
var state = IDLE
var Axis = [Vector2.RIGHT, Vector2.LEFT]
func _ready():
randomize()
func _physics_process(_delta):
if state == IDLE:
if $TimerIdle.is_stopped():
velocity = Vector2.ZERO
$TimerIdle.start(randi_range(1,3))
$AnimatedSprite2D.play('Neutral')
elif state == WANDERING:
if $TimerWandering.is_stopped():
$TimerWandering.start(randi_range(0,4))
velocity = Axis.pick_random() * idlespeed
$AnimatedSprite2D.play('Run')
elif state == ATTACK:
$TimerIdle.stop()
$TimerWandering.stop()
position += (player.position - position) / runspeed
$AnimatedSprite2D.play('Run')
move_and_slide()
func _on_timer_idle_timeout():
state = WANDERING
func _on_timer_wandering_timeout():
state = IDLE
func _on_detection_area_body_entered(body):
if body is CharacterBody2D:
player=body
state = ATTACK
func _on_detection_area_body_exited(body):
if body == player:
player = null
state = IDLE