Welcome back, my fellow game developers.
As you can see, we've created some spikes in here.
So now when we have these bikes,
if I stand on them, there we go.
I get damaged. And if I stand and go back,
I don't get damaged.
And if I stand and keep standing, I get damaged.
But now because I'm still invincible,
I get damaged again.
If I keep standing in here,
so I might die,
I'm going to go away a bit
and let's not waste any more time.
And let's get started.
Oh, okay, so let's get to spike creation.
And I've already created the spikes and everything.
Now, this is something that I do not like to do.
I don't like to do things off screen and then tell you,
but because I wanted to make sure that
this video is as efficient as possible,
It's a long and somethings will need to be explained.
So for that reason,
I created all the animations and so on.
I'll just give you a quick tour.
So what I did, I chose one of
these three traps right here.
I went back in the game.
I selected the sprites,
I'm sure it's 32 multiple,
made sure to edit the sprite and sliced up 32 by 32,
made the filter, and so on.
I then went ahead and created this spike trap,
which has a sprite renderer.
A animator will get to that and
a box collider that is just around it,
which is also a trigger because we
want the player to walk over it.
Okay?
Now I created also an animation for the spike.
One is idle, one is popping,
so the idle just stands there, does nothing.
This will be the default animation.
And then we have the spikes popping,
which is in here.
You'll see I created one to pop this private.
So there you go. You can see,
and why would I make it a bit slow?
I just wanted to give a chance to the player
to think twice before stepping on there again.
So it has nothing to do with
the player wants the player
stops and it starts triggering.
And then he has a couple of frames.
Can move this right here.
He has a couple of frames to realize
his mistake and then try to run away from it.
With that out of the way,
let's talk about the animator and what we've done here.
So I created the ival,
made it the default layer state,
and then created the two transitions,
one going 21, going from.
I also created a parameter in here which
is a boolean for the BOP spikes.
And once the pop spikes is true,
then we go from idle to Pops spikes.
We don't have any transition duration,
we don't have any exit time.
But when we are going back from the bopping,
we do have an exit time which is
about 90 percent of the animation.
We can even increase it to one maybe until
we finish the entire animation and then we go back.
I am rather make it 0.57.
But for some reason when I set it for this,
the animation in here disappears.
I'm really confused why this is happening.
I don't like it at all.
So 92 maybe in 1906.
1907 just makes everything disappear,
which is extremely annoying.
And I have no idea why this is happening.
But anyways, we have an exit time
which is 96 percent of the animation.
Then we go back. Well that out of the way,
if you do one, pause the video right now,
go do everything for that.
Create all the animations and so on.
Because I'm going to go ahead and start
in the scripts by creating a,
let's call this folder the traps.
Maybe you'll want to create multiple traps.
And in here I'm going to create the spikes.
So right-click, create the C-sharp,
and let's make it the spikes hit Enter,
select the spike trap.
And I'm going to drag these parks in here.
Now I'm going to double-click and
open it up and Visual Studio.
Now, before we continue on,
let me just explain
what the main idea or how this spike is going to work.
Because in just a bit I'm going to issue the challenge
and I want you to know how the spike will work.
So the player is walking around,
whereas the player, they don't player is walking around.
Then suddenly while the spikes are hidden, hidden,
he walks over on the spikes and they start popping out.
So when they do hit him,
what we want is we want
the animation to have some kind of
event system and that event system will damage the blade.
So we'll need to have
the damage amount variable will have to get
a reference to that layer and we
have to add an event in here that will damage him.
We also want to make sure that if
the player is still standing on the spikes,
we want to keep damaging him.
Not just get damaged
once and then skip standing in here and not feel a thing.
We also want to make sure that when the player
activates the spikes, he goes away.
We don't want him to keep getting damaged because while
the animation of the spikes is
working and events system will still get triggered,
or you'll see how everything is
working when we actually create that.
But until then, I'm going to issue a challenge
to damage the deep layer with spikes.
So first of all, create a variable for the damage and
the player we should damage
will need to create the entrepreneur Enter.
You'll need to start the animation
and make sure it stops.
So make sure it stops means that we
may need and on sugar exit.
Then you'll need damage
the player using the event system.
And finally, I just want you to
think of what should happen if
the blur stays on the spikes and then exits.
So with that out of the way,
pause the video right now and go do the challenge.
Okay, welcome back.
So back in our script in here,
we are going to get, first of all,
a serialized field which is going to be of
type integer and the damage amount.
Then we are going to create a private void.
And there's no excuse me, not a private void,
a private collider 2D,
and this will be the player to damage.
Save that.
Now I'm going to remove the start and
update if we do need them later on, we can add them.
It's pretty simple.
I'm going to create the On Trigger Enter to D.
I'm going to check if this
collision.com tag is the layer.
If it is, then I'm going to get the component which
is the ending main door.
And I'm going to go ahead and set the bool,
which is going to be the letter
because this is a string reference.
We are going to get this bikes.
Bob spikes, copy that.
And back in here.
And let's set this pops.
Bikes, save that.
And then we'll create a method in
here which is going to be to damage the player,
which is a public void damage layer.
And also we need to set this to true.
Save that.
So we are going to damage the player.
What should we be doing in here?
We are just going to get the player to
damage dot get component,
the player health handler.
And we are going to go ahead and
damage layer with the amount of damage.
And before we forget,
how are we going to get access to
this layer damage when the player steps in here?
What's the problem in here?
Amount of donors not exist.
Oh, so sorry.
Damage amount.
Where did I get the amount of damage?
I'm not even sure anyways, in here,
the player to damage
is going to be equal to the collision.
There we go.
So on Trigger Enter,
we check, is it the player?
Yes, we start the pop spikes animation.
Then we set the player to
damage as the collision because it's declared,
we know that then we are going to damage the player.
And finally, we are going to go back in here.
What's the problem?
Okay? Nothing animation and we are
going to select in the scene.
So there you go.
We can see that it starts off, then nothing happens.
And a couple of seconds we wait and then we book in here,
I'm going to add the event
and I'm going to select to damage the player.
We're going to save that to run the game.
And we shall see in here that we have a life
of 600 once I stop on these bikes.
Yep. There we go.
It damages us.
It doesn't damage us because if I go back to the spikes,
we don't have anything to damage.
Let's just set it as 100 for now because the life is 600.
So I think he can take a couple of hits.
So now we have the spikes.
Yep, there we go.
You can see that it removes 100 every single time.
And because we have this
invincibility for a couple of seconds,
the second tip doesn't affect the layer.
But the problem is,
you can see that it keeps
playing and I keep getting damaged.
So the player died even though he's standing
outside the perimeter of the sparks.
So what we should be doing is first of all,
we need to be stopping the animation On Trigger exit 2D.
And in here, I'm just going to do
the same thing I did in here.
Check for the player.
And if it is the player that just exited the collision,
we are going to set this to False,
save that back in our game.
And there we go and run the game.
So now we step in here,
we get damaged, we step outside.
There we go.
We can see that the animation stops,
but there is something when I stop and go back,
you can see that even though I'm outside, so step back.
I am still getting damaged.
Why? Because the animation in here is still playing.
Events system is still getting
triggered and we do know what the player is.
So to avoid that,
I'm going to create a public bool.
And this public bool just so we can see it in
the inspector will later on set it to private.
And I'm going to call this
the give player or damage player,
or shouldn't damage player,
I believe is a better name for this.
Verbs should damage player.
And once he is inside,
then the should damage player is equal to true.
When he's outside the should damage player is false.
And then inside of the damage player method,
I'm going to check if we should manage the player.
Then we are going to go ahead and
access the player health handler and damage the player.
So now back in our game,
we should see that when I run the game,
we should not damage the player.
Once I step in here, we should
damage the player, we get damaged.
But if I activated and then go outside, there you go.
You can see that should damage player book, book.
There we go.
You can see that I am no longer affected.
So I hope you enjoyed this video.
I hope you enjoyed this section.
I hope you are always staging your files,
committing your changes, creating reviews.
And then in the next section we will
see where this journey takes us.
So I'm going to go ahead and say in
here that I created spikes,
that damage layer, commit the changes.
And I can see right here and
the masters how our game is moving along.
Look how much we did,
and look how much we still have to do.
This game is just getting started so I can not wait.
Let's not waste any more time and I'll see
you in the next video.