Making an enemy attack during a specific animation

Godot Version

4.2.2.stable

Question

Hello! I am making a 2D platformer game, and up to this point have had standard pathing enemies that walk back and forth. Now, I am trying to create a boss which will have actual attacks and corresponding animation, but I’m not really sure where to begin with that process. Currently I have the boss set as a StaticBody2D, but I’m unsure if it will need to be changed to a CharacterBody2D (I do not plan to have the boss move, only throw projectiles).

My code for my boss looks like this currently:

extends StaticBody2D

class_name Flamagor

@onready var anim = $AnimatedSprite2D
@onready var enemExpSpawnPoint: Marker2D = $Marker2D
@onready var flamagor_collision = $CollisionPolygon2D
@onready var flamagor_dial_intro = %Intro
@onready var flamagor_dial_death = %Death

@export var experiencePoint: Node


var health: int = 50
var max_exp_spawn = 20


func _ready() -> void:
	anim.play("flamagoridle")


func apply_damage(damage: float) -> void:
	health = clamp(health - damage, 0.0, 50.0)
	if health >= 1:
		anim.play("flamagortransformidle")

	elif health <= 0:
		#anim.play("flamagordeath") (needs anim)
		flamagor_dial_death.playing = true
		await get_tree().create_timer(9.0).timeout
		flamagor_collision.disabled = true
		GlobalVariables.flamagor_dead = true
		drop_health()
	var number_exp: int = 0
	while number_exp <= max_exp_spawn and health <= 0:
		drop_experience()
		number_exp += 1
		queue_free()


func drop_experience() -> void:
	var point = load("res://scenes/exp.tscn")
	var instance = point.instantiate()
	owner.get_parent().add_child(instance)
	instance.transform = enemExpSpawnPoint.global_transform


func drop_health() -> void:
	var health_point = load("res://scenes/healthpickup.tscn")
	var health_instance = health_point.instantiate()
	owner.get_parent().add_child(health_instance)
	health_instance.transform = enemExpSpawnPoint.global_transform


func _on_animated_sprite_2d_animation_changed() -> void:
	flamagor_dial_intro.playing = true

Like I said, I’m not really sure how to begin as I’ve only coded basic enemies thus far. Any help is appreciated, even to get me going the right direction!

Thanks in advance.

Animations can call functions. So if you need to spawn something at a particular frame have the animation do it.

Also look into AnimationTrees they have a lot of nice features that will allow you to avoid writing a lot of logic code.

1 Like

I’ll admit I’ve stayed away from animation nodes/trees, as they overwhelmed me a little bit compared to the sprite frames in the AnimatedSprite2D (when I first started). I will have to look more into them then, as it sounds like that will save me some of the headaches I’ve experienced already. Thank you for your response!

I will also add that if this is like a Boss and you need a sequence of attacks. Animationtree can easily setup an attack loop. Although you may need to get a little creative to have randomized patterns. I would still leverage the animationtree irregardless.

I think this video is decent.

1 Like

This is a great video, thank you for sharing! I am going to deep-dive into these, as this will definitely solve my problems.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.