Daily progress: Spider Attack

Esteban Ibarra
5 min readAug 25, 2021

--

Trigger warning for anyone afraid of spiders, you may want to skip this one.

The spider enemy while inheriting from the base class, won’t really move, but will just stand up and spit out acid globs.

As we begin, the spider currently has the patrolling behavior of the other two enemies:

In the animator, remove the Walk animation and just leave the idle.

Next, we create the Attack animation: Select the spider's sprite, add a new clip, name it Attack, save it in the spider's animation directory, drag the attack frames into the animator's timeline, set it to around 20 samples.

If you'd like a slower more detailed explanation, try one of my older articles, all will be revealed there!

Next, make a transition from idle to attack and back. 0 transition times.

If we run the game, we’ll see that the spider is moving as it stands up. That’s because it’s still running the patrol code, we’ll have to override it in our spiders code:

Since we override the base’s movement code and do nothing but have a comment and the base movement commented out, nothing is being run and so our spider will no longer move!

The spider will spit out what we’ll call an Acid Effect, A new animated sprite is created called Acid and a prefab is made of it:

For the attack, we’ll use an animation event to instantiate the acid glob.

So we go to the attack frame where the spider is glowing, and we add an event. This will help us attach a scripted event or function that tells the spider, “Hey, shoot one of your acid globs!”

First, create a new script called Spider_Animation_Event.

This will be the script that will catch the event from the animation window and tell the spider to fire.

I got a little ahead of myself and called a function in Spider that doesn’t exist yet, but I knew I would be attaching the script to the sprite, which is why I used GetComponentInParent. Speaking of which, Attach the script to the Sprite component to be sure the event would catch the available functions.

Oops, I already had the script attached! Pay no attention to the man behind the curtain as I delete the extra.

If all goes well, the Fire() function will appear in your Animation Events function slot, go ahead and choose it.

In the spiders code, we’ll instantiate the AcidEffect.

This is a basic instantiation, we’ll have it appear at the spider's position and use Quaternion.identity for rotation.

We’re almost done. If we run it now, nothing will happen because we haven’t drug the prefab into the holder, and we need to write a script for the acid effect that moves it as well!

First, drag the prefab into the variable holder:

Next, we’ll write the AcidEffect script, With the AcidEffect, there’s 3 things we want to do: 1) Move 3 units a second, 2)Detect the player and deal damage, and 3, self destruct after 5 seconds.

What I’ll do first is put the speed and time for self destruction into variables:

The self destruction part is easy enough:

So is the movement…

For detecting the player and causing damage, we already have a damage system in place, we’ll need to give the player code an IDamagable interface!

Of course we’ll now have to add Health{get; set;} and a damage function.

We’re now ready to give the AcidEffect its damaging powers! Of course we’ll have to give our AcidEffect sprite a boxCollider2D and set it to trigger:

Now in our AcidEffect script:

If the player enters the boxCollider, the boxCollider will know its tag, and if it’s the player, it will then grab the IDamagable interface and place it into the hit variable, after a null-check, it will call the players damage and destroy itself. pretty straightforward!

And sure enough, if the AcidEffect hits the player, the player now does get hit!

--

--

Esteban Ibarra
Esteban Ibarra

Written by Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.

No responses yet