So let's start by adding the enemy script to the enemy game object.
So I'll go ahead and select the enemy game object and scroll down and
Add Component > Scripts > Enemy.
Now as you can see, the enemy script has several variables that need to be set up.
Move speed basically defines how fast the enemy will move
when it's moving through the waypoint system that we'll set up later.
Damage amount is how much damage will be dealt to the player.
This is set large currently by default so it will immediately kill the player
based on the defaults of Sparty, which I think had essentially one hit point.
The next variable is stun check.
And this is looking for a game object.
Since we already are using a collider on the enemy to prevent the enemy from
falling through the ground, we can't really differentiate a second collider for
the enemy's head.
So stun check is looking for a game object that we'll use to detect the enemy's head
collision, essentially a child object of the enemy.
So I'm gonna go ahead and create that now.
So with the enemy selected under the hierarchy, I'll select Create Empty Child.
And I'm gonna go ahead and rename this HeadCheck.
And we need to add a Box Collider 2D component
to actually detect the collision on the head.
So let me go ahead and do that.
So I'll add Physics 2D > Box Collider 2D.
And of course, this outline is the entire player currently.
I need to edit this collider to basically be just the head of the character.
So, wherever I want Sparty to actually be able to stun the character.
So let me go ahead and modify that.
I want it to be just sort of a little pancake here that Sparty will be able to
jump on and land.
We actually wanna make this collider a little bit smaller
than the colliders on the enemy.
So if I go back to the enemy,
notice we have this box collider that we previously set up to detect
if the enemy is touching the player in essence for attacking the player.
This was set as a trigger.
Of course, I'm gonna actually edit this and make this slightly bigger just so
the player if they just run up to the enemy, they wouldn't accidentally hit
the collider of the head before hitting this box collider, which is the attack.
So basically, Sparty would have to come in from above and
hit the head and couldn't come in side to side to stun the enemy.
Okay, so back on the head check, we've got our box collider set up.
This one I'm not gonna make a trigger, I'm just gonna leave it as a regular collider,
so Sparty will actually be stopped when he hits this.
And I do wanna actually add a Rigid Body 2D to this as well,
because it's got a collider, and this object's gonna be moving.
So for optimization purposes, we should probably add a Rigid Body 2D to this.
So we'll go ahead and do that.
But I'm gonna set this to Is Kinematic,
because I don't want this to be driven by physics.
I don't want it to follow gravity and things like that.
But I am detecting a collision here, it could be a fast paced collision, so
I'm gonna change Collision Detection from Discrete to Continuous.
And I'll go ahead and freeze the z rotation for good measure.
And the last thing that I need to do on my head check is actually add the enemy stun
script that's provided, so let me Add Component > Scripts > Enemy Stun.
This script detects the collision between the player and the enemy's head, and
it basically notifies the enemy that it was collided with on the head and
it should be stunned.
We'll actually look at that script a little bit later.
So moving back to the enemy game object,
let's continue to look at the properties here.
We've got the stun check now so let's go ahead and set that up.
I'll drag the HeadCheck to the Stunned Check property.
The Stunned Time is how much time in seconds the enemy will be stunned.
Right now, we'll just stick to the defaults.
The Stunned Layer is basically the layer that the enemy will be moved to
when stunned.
And you can see this defaults to something called StunnedLayer.
We actually haven't created that layer yet so let's go ahead and do that.
So up under Layers,
notice my enemy is currently on the Enemy layer as we've set up previously.
So let me click on this and go down to Add Layer.
And then, just in the next available spot,
I will type StunnedEnemy, one word, capital S, capital E.
Now back on to enemy, I'm not gonna actually change this right now to
the StunnedEnemy layer, I want it to remain on the Enemy layer.
But the enemy script actually will dynamically change it to that layer
when it's stunned.
The next property is the name of the Player Layer,
which of course is just Player by default and I'll leave it that way.
Basically what the code does is it says ignore collisions
between the player layer and the stun layer.
Once again, we'll look at that in a moment.
And the Is Stunned is basically Boolean,
true or false, if the enemy is currently stunned or not.
And the next few variables are all about the waypoint system for the enemy moving.
We'll come back and look at this in a future lecture.
But for now let's go ahead and set up the sound effects.
So in the audio folder, I've got an enemy stunned sound effect or an enemy
hit sound effect, and I have an enemy attack sound effect for the attack sound.
So we have our enemy script component variables all set up, so
let's go ahead and test this.