Working Animation Events

Creating a firing feature for the Spider Enemy

Simon Truong
4 min readDec 19, 2021

--

The Spider enemy is going to be the special one since it is going to be able to shoot projectiles at a certain keyframe in the Animation. There is going to be two parts to this feature, first is going to use Key frame animation events to trigger the firing sequence. Second is going to be the actual projectile that is going to travel towards the right and after a certain duration it will destroy itself.

Creating the Projectile Asset:

Just like how we have imported and sliced all our previous sprites we will do the same for our Acid projectile sprite. Drag the first frame into the scene and name the sprite to “Acid”. Create and animation for this new game component and import the rest of the frames as the animation sequence.

Finally we are going to make the Acid projectile as a prefab so later on the spider can Instantiate the game object.

Acid Prefab Behavior:

Time to create the Behavior of the Acid projectile. What we need is for the projectile to travel right for a set amount of seconds before it can destroy itself. Along with it being able to damage the player upon a collision.

Start by creating a new C# script called “Acid Effect”. Since we are going to be using the damageable interface we need to add the IDamageable on the top beside MonoBehavior.

Since we are going to be utilizing the transform Translate we will also need a speed variable.

And since we are going to destroy this element within 5 seconds we will also need to add an Coroutine element to start the countdown.

Finally make sure to drag the script into the Acid prefab.

Spider Animation Event Script:

We are going to create a new C# script which will allow the firing of the projectile at the precise moment the spider starts to glow. The new C# script should contain a public method called Fire. (we are going to leave it empty for now). Then drag the script and attach it to the spider SPRITE object, or where you have your animator component attach to.

Once you have attached the animation event script we are going to create a new animation event tag. Slide and locate the exact frame which the spider is going to fire or start glowing, then click the Add event button just left of the dope sheet. This should tag the keyframe with an event on the top bar of the dope sheet.

Select the event animation and in the Hierarchy we can select the newly created script “Fire”.

Spider Script:

Now that we have our projectile and Fire trigger. We need to tell the spider script to instantiate the project. The logic behind this sequence is that whenever the Function “Fire” is called in the Animation event, it is going to trigger a public method from the spider script to instantiate the Acid projectile.

To start, in the spider script we are going to create a Serialized Field for the gameobject called “Acid”.

Then in a new method called “Attack” we are going to Instantiate the game object. Make sure that you drag and drop the Acid prefab into the game object slot.

Spider Animation Event:

Back to the Spider Animation event script, since we left it empty, we need to replace that with the Spider Attack method. To start we need to get access to the spider script which is on the parent object.

Then in the Fire method we are just going to call the spider public method “Attack”.

With that setup, everything should be linked together seamlessly.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.