Creating Enemy Attack Behavior in Unity

Unleash Aggression with Animation Hitboxes and Override Methods

Micha Davis
5 min readJun 5, 2021

Now that my modular enemy script is complete, I can start working out some unique behaviors for each of my three enemy types. Specifically, I have to different kinds of enemy attacks: melee attacks and ranged attacks. No enemy is going to have both kinds of attack, so it doesn’t make much sense for me to load this behavior into the Enemy base class. Instead, I’m going to make the ranged attack unique to the enemy that uses it, and then create a general attack script for the other two enemy types.

Melee Attacks

As you can see from the header image, the melee attack is based on an animated collider that follows the sprite through its attack animation.

To set this up, we’ll need to create an empty object with a BoxCollider2D. In the Animation Dopesheet, we record the movement of the hitbox through each frame of the animation where the weapon is swinging.

We also need to deactivate the collider component at the correct frames so that the weapon is only dangerous when being swung. All the changes you make to the object while recording will be entered as animation properties on the Dopesheet.

There’s no need to create different attack behavior for everyone — we already made an Attack class for our player to use. We can attach this class to anything that can cause damage — in this case, the enemy and player hitboxes.

One thing we have to do is isolate everyone’s attacks onto different layers. It wouldn’t be good if every swing the Skeleton (or the Player!) was calling their own Damage method. We can use Physics layers to prevent this.

Start by making layers to isolate the enemy, the player, the enemy’s attack, and the player’s attack. Assign each enemy to the Enemy layer, and their hitboxes to the Enemy Attack layer — likewise for the player, the player’s hitbox and their respective layers.

Next, open Edit -> Project Settings and select the Physics 2D menu. At the bottom is the Layer Collision Matrix. Make sure the Enemy column has Enemy Attack deselected, and that the Player column has Player Attack deselected. Now the collision detection for these layer combinations will be disabled.

Finally, to make the attack happen, we need to add the logic to our Animator state machine. We’ve already scripted an InCombat bool, so all that remains is to make sure that idle redirects to the attack animation if InCombat is true.

That’s it for melee attacks. The same simple process can be repeated for any other melee enemy.

Ranged Attacks

For the Spider’s acid blob ranged attack I need to create two scripts: one for the acid blob’s behavior, and one to use as an Animation Event that triggers the Attack method in the Spider class. The Attack method instantiates an acid blob prefab with the Attack script attached to it.

Let’s look at each part in turn.

First, we need the SpiderAnimationEvent C# script. Inside, we’ll include a Fire method that signal’s the Spider class’ Attack method.

Add the SpiderAnimationEvent to the object with the Spider’s Animator component. In the Animation dopesheet we’ll add an Animation Event to the attack animation frame where we wish to instantiate the blob.

Once that’s done, you can view the event in the Inspector. Select the Fire() method.

The Spider class’ Attack method simply calls for the instantiation of the blob prefab:

And the Acid Blob behavior is also pretty basic. I’m just giving its rigidbody a little physics push and then destroying it after a few seconds. I’d like it to have a ballistic trajectory, so I’ll keep gravity on and maintain the rigidbody’s y-axis velocity in movement.

Finally, I put the Attack script on the blob prefab, and assign it to the Enemy Attack layer, so that it can damage the player properly.

I don’t really need to worry about triggering an attack state for this monster — they can just loop back and forth between idle and attack, spewing acid until the Player comes to stop them.

That’s all for enemy attacks. Thanks for reading. Tomorrow I’ll cover setting up the loot system.

--

--

Micha Davis
Micha Davis

Written by Micha Davis

Unity Developer / Game Developer / Artist / Problem Solver

Responses (1)