Niaboru
Niaboru

Reputation: 1

Enemy animation problems godot

I have created an enemy for my 2d rpg game in godot but I used AnimationPlayer and don't know how to make my enemy folow me with the right animations

onready var sprite = $AnimationPlayer
onready var stats = $Stats
onready var playerDetectionZone = $PlayerDetectionZone


func _physics_process(delta):
    knockback = knockback.move_toward(Vector2.ZERO, FRICTION * delta)
    knockback = move_and_slide(knockback)
    match state:
        IDLE:
            velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
            seek_player()
        WANDER:
            pass
        CHASE:
            var player = playerDetectionZone.player
            if player != null:
                var direction = (player.global_position - global_position).normalized()
                velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)
    
    velocity = move_and_slide(velocity)

func seek_player():
    if playerDetectionZone.can_see_player():
        state = CHASE


func _on_Hurtbox_area_entered(area):
    stats.health -= area.damage
    knockback = area.knockback_vector * 65


func _on_Stats_no_health():
    queue_free()
    var enemyDeathEffect = EnemyDeathEffect.instance()
    get_parent().add_child(enemyDeathEffect)
    enemyDeathEffect.global_position = global_position

Upvotes: 0

Views: 271

Answers (1)

Theraot
Theraot

Reputation: 40170

The AnimationPlayer has a play method you can call to tell it to play an animation (you pass the name of the animation as parameter). Usually you would make looping animations and simply call play to switch to a different animation when necessary (e.g. when the character changes states).

I don't see you use the AnimationPlayer at all. Plus I don't know which animations do you have, or if you need anything more to make it work for your case. So, I'm limiting myself to the very basic.

Upvotes: 1

Related Questions