In this lecture, we're adding hazards.
So when the player touches the spikes whomp, they trigger the death animation.
Let's go through this process together as setting up our hazards tile map and giving it all the components
and then making sure that we've got things ready to go.
I'm going to give this to you as a series of little Q&A type quiz questions, micro challenges, if
you will.
We'll see how we go.
So the first step, what do we need to do?
First of all, actually to create ourselves a layer to put hazards on.
Well, we need to jump over into toll map grid, right click, find 2D objects, tile map and then rectangular.
We'll call this hazards tile map.
Okay, that's the starting point.
Now, what do we need to do in terms of layers when we create our new tile map?
That's a good chance for you to pause the video and do those things if you want.
That'd be cool.
We can do it together and you can see if you can follow along while doing it the same time.
I'm going to go out to layer on the hazards tile map, click on the default, their add layer and we'll
call this hazards in our user layer 12.
For me, I'm also going to add a sorting layer.
Click on the plus button.
These all call also hazards.
We could probably do it at the same as enemies, but you might want the enemies to run in front or behind
of the hazards.
So we're going to have this zone layer and for now I'll put it in my stack here it'll be layer five,
which is just above enemies, meaning enemies will run in front of the hazards.
Okay.
So next, what do we do with these layers now that we've made them?
Where do we need to go and set them?
Well over into hazard's tile map.
I'll change the layer to be hazards and then we'll find our time map renderer and we'll change the sorting
layer to hazards as well.
Okay.
Doing well so far.
Next up, go and find the spikes that I gave you way back when you got these assets.
These are some more some pretty bad, Rick.
Art, if you think you can do better, then by all means create some nicer spikes.
If you're not sure how, then you can jump into our pixel art course and learn a few things there.
I'm going to click on the Sprite editor because I need to make sure that the physics shape is suitable.
So I'll click on the Sprite editor, top left custom physics shape.
I'm going to drag loosely around my object.
You might want to drag in the middle.
I found if you drag off the screen, it doesn't work.
So drag somewhere in the middle yet four points bottom left.
Bottom.
Right, and I'll just put this up at the top here a little bit.
Now, just right, touching the spikes, I think that's fine.
Doesn't matter if there's not a gap in the middle.
We don't need to follow the contour of these.
Exactly, because the player is going to generally jump on top of these, even though there's two.
And anything that matters, click on apply that should be good.
Ex out of that.
Now what else do we need to do on our hazards tile map to make sure that we can collide with our spikes.
We need to go to add component and we'll add a tile map collider to DD so then it knows anything we
put in there should have collision on it, which is great.
Now you know what I'll do now is I'll just dig out a couple of holes to put our spikes in.
So for me, I'm going to go to my platform's tile map.
I'll click on my rule tile, give myself a little extra ledge here to jump up there.
I'm going to hold down shift to delete some spots.
So that's how you can delete delete some spots in there that I can put some spikes in maybe a couple
of spots up here as well.
You know, we'll make this a big, old, big old pit with spikes down the bottom of it.
That looks pretty good.
Next up, we need to be able to paint the spikes on to our hazards tile map.
So what do we need to do there?
How do we go about doing that?
Well, hopefully you're following along.
You're yelling at the screen or you're actually just doing this.
I'm going to drag the spikes into my palette and then I'll ask me where we want to save it.
Assets.
We will save these in.
Let's see.
Tiles I think is good and just called spikes.
Excellent save.
You can see we've got spikes down here.
Now if I change my active tile map, very important to hazards, click on the spikes and I can paint
those in the spots.
And the good thing is I can see the green collision around it knowing that I should be able to bump
into those, collide with them.
I've got a couple of spikes in there.
I'm happy with those.
Now there's one more main step we need to do in here.
We need to say in our code that the player can collide with this particular layer.
So how do we go about doing that?
I'm going to click on hazards, go to my add layer and just double click on hazards and copy that to
make sure I get the spelling exact.
Because this is hitting me a couple of times that I've got it not exact and haven't figured out what
was going on.
Find my scripts, jump into the player movement script.
Now if we scroll down to die, we can see that we've got our collider touching layers, we're touching
enemy.
We can also comma quotation marks and paste in hazards in there.
So if we're touching enemies or hazards, we should to all of our dying related things.
So save back over into unity.
Let's see if this is working.
Click on play.
Run and run and jump.
And we're not dying now.
I think I know what's going on here.
And it's something that I identified a little bit of a bug just a moment ago.
If you have a look at our player and our spikes, we're not getting to the point where we can actually
get our capsule collider to touch the spikes because our feet are preventing it.
That's because our feet is a collider, not a trigger, and the spikes are a collider, not a trigger.
So we're not falling through these to get to the point because our we want to have our feet on the very,
very bottom of things.
So the way to resolve this is we can just make our hazards colliders.
And so if we click on our hazard tile map and change that to let's see the tunnel map collider change
it to is trigger.
Now if we click on play, that means we should fall through it and we can actually touch our collider
to it.
Boink and then we go, we die.
Let's have a look and see what the behavior is with the enemy.
So I have a suspicion the enemy has its collider or that that is going to prevent us actually getting
to our collider as well.
So I think I didn't test this in the last lecture.
Apologies for that.
I think if we jump on the top of our enemy, we're not going to die straight away, okay?
And then we die when it finally touches our capsule.
Collider So same thing again.
I'll click on the enemy and I'll change the enemy's capsule.
Collider To be a trigger so that our feet can fall through it.
I don't think there's going to be anything else that is going to be affected.
It's fine if the enemy is a trigger, we're going to do have things trigger.
So let's see with that, I think I might have a little bit of a bug with it turning.
Not probably.
Yeah, I will.
But let's just see if we can land on it.
First point, we land on it, we die straight away.
So that part's fixed.
Why are we doing the jiggly?
Jiggly?
Well, I think I know what's going on here.
I'm going to show you a little technique.
If you click on play, get ready on the pause.
Just before we get to the end.
I've clicked pause and you can step through, step through, step through.
You can see that the reverse periscope is going through it.
It exits the collision.
We flip.
But now this that we've made a trigger is touching is inside this collision.
So I just flipped and that's in there.
But then once it exits, it flips back again.
So we just need to deliver of spacing, which should be okay, I'm going to get the reverse periscope
and click on Let's See the Box Collide.
I click on edit, just move that out a little bit further.
So it's going to turn a little bit sooner just to there should be good and we'll move the capsule.
Collider So it's just a little bit less spacious around the outside.
It doesn't need to be quite so high either.
So I'll put the collider in a bunch here and all.
We could move the periscope out a little bit more and the enemy flips around because the periscope is
doing what it's supposed to be doing.
Let's see if we can now jump onto the spikes we can forward.
It flings us.
Good, good reaction there.
We'll see if we can hit the enemy and if it causes our death animation, then we're all good.
Jump on top of it.
Great.
So there we go.
We've got our hazards and we've got our player.
Oh, you know, one last thing I'll show you is the fact that anything we put on this hazardous tile
map becomes a hazard that will harm the player.
So say all this water up here, instead of the water being on the water tile map, it's going to click
on the water tile map and I'm going to find my tile palette, click on the water, I'll click on anything,
hold down, shift and delete.
That's just an easier way.
Deleting then getting the eraser.
Now I'm going to click back on the hazards.
Tile map has its tile map just here.
I'm going to click on water.
I'm going to add the water into the hazards tile map.
Now, when I jump over and touch the water, that should cause some sort of death reaction as well because
it's now considered a hazard.
Okay, that's getting funny as opposed to considered water.
So it's up to you whether you want the water to be a hazard or whether you want the water to be something
you can walk through or maybe even swim through if you want to play around with that functionality for
now, I'm going to leave it as a hazard.
That means the water tile map doesn't have anything on it for the moment, but that's okay.
We might do something with that later on.
I'm going to have it on the hazard.
So the challenge I've got for you is to play some hazards in your level if you haven't done so already,
experiment with different objects, place some hazards in your level.
You can have some water, you can have some spikes, you can create your own things, be a little bit
creative, and when you've added some hazards, you're ready for the next lecture.
So I will see you in that next lecture.