Godot Version
Question
I am working on a project that has a few enemies, and each enemy has a state machine. One of these states is the chase state, and it get’s triggered with an Area2D node’s body entered signal. Once triggered, the base class of the enemy gets the triggering body added into an array that I can access within the chase state, with that I calculate the direction and move the enemy towards the target.
One of the nodes that all enemies share is an AnimationTree node. I thought that this would be the best solution for animations in general and that it would simplify my work. However, I was just attempting to implement an aggro animation just before I send off the enemy to chase down it’s target, and it seemed simple enough before I started. I used the entered_state() function to play the animation, wait for it to be finished and then set a can_chase bool variable to true so that the movement in the physics_process function is allowed to start.
Even though it is working, it feels like the AnimationTree is just adding extra steps in this case. If I had only an animation player I could use a aggro signal on the enemy class and just play that animation with that signal directly on the AnimationPlayer node. It seems like I’m using the AnimationTree poorly… Could anyone help me out with a suggestion for better modularity and just cleaner code?
Here is some print screens of nodes and some relevant code:
The current conditions used to change animations:
My current AnimationTree setup for Enemy:
Chase state’s enter_state():
func enter_state():
if self_body:
self_body.velocity = Vector2.ZERO
var aggro_dir : Vector2 = (self_body.hostile_targets[-1].global_position - self_body.global_position).normalized()
self_body.animation_tree["parameters/aggro/blend_position"] = aggro_dir
self_body.animation_tree["parameters/conditions/has_new_target"] = true
await self_body.animation_tree.animation_finished
self_body.animation_tree["parameters/conditions/has_new_target"] = false
can_chase = true
self_body.current_direction = aggro_dir

