In this lecture, we're adding collision to our world and collision to our characters so that we can
drop to the ground using gravity and then stand there and not fall over.
So let's jump in and get started.
I think it's time for us to pre-fab our players, so I'm going to jump in my assets directory, right
click, create a folder and we will name this prefabs.
It's good to have all these things as prefabs because if we want the player to go from level to level,
we need to have one player prefab that we're using.
So I'm going to open up prefabs folder, grab my player, just drop the player down here makes a prefab.
Excellent.
Now because we're going to be making some changes to the player as well in this video.
So make sure you pay attention to whether you're working on the prefab or working on the instance or
what you're working on.
Just a note of that.
Now let's turn our attention to the tile map itself.
We want to add some collision on.
There's a couple of nifty things we can do.
But first of all, just a note that we can apply the collision to any of our particular tile maps.
So eventually that three tile maps in our scene at the moment and see we can change it by either selecting
along here or we can click on the object in the hierarchy itself.
So we want to be putting the collision on platforms.
So when the player is touching these platforms, the player lands on them and can jump on them, etc.
Not on the background and not on the water.
Maybe the water bit.
Probably not for the moment, but definitely on the platform tile map.
So while we've got that clicked in our inspector, I'm going to scroll down ad component.
I'm going to start by adding a tile map collider to DX and that you see will add collision into all
of our blocks, all of our tiles that we've placed in the scene.
That's a good start.
We could potentially finish at this.
But there's one issue that I've found is if you scroll all the way in, all the way in, all the way
in, we'll see how far it's actually going to let me scroll in here all the way in.
You can see that there's a gap where you can't see, but there is a gap between this tile on the left
and this tile on the right.
The collision is this tiny little gap in there, and sometimes we trip on it.
We can now foot stuck in it and things don't go so well.
But there's a cool thing that tile map lets us do if we jump down into add component again, if we search
for composite.
Collider 2D, composite.
Collider 2D, that allows us to join all these things together.
So when we added the composite collider component, it's also added a rigid body.
All we need to do here is set this to static because our world is not going to move.
It's going to stay where it is, the platforms in the future.
We might do some moving platforms, in which case we wouldn't do it as static.
But for now all we're doing is just having it sitting there stationary, make it static, and then it's
going to roll up rigid body to DX so it's not getting in the way under tile map.
Collider 2D You can see that we've got a used by a composite.
If we click on that, then our entire world is wrapped, our tile map is wrapped with our collision.
I'm just going to turn off for a moment the tile renderer, just so you can see, we've got this green
outline now that is our collision that goes around all of our tiles.
And if we're to get there and paint using our rule tile, paint a new tile just in here, then you can
see once again, I'll just hide my visualization.
You can see that our collision responded to that and wrapped around in a really nice, dynamic way.
Super handy.
It couldn't be any easier than that, huh?
Just very straightforward.
Now, what we need to do is make it so that our player can actually go bump and fall and land.
Because when we kick click play, he's just standing there doing not much, just floating in space.
So I'm going to give this to you as a challenge.
And that challenge is to make your player fall and land that make this even clearer.
I'm going to grab the player, hit W and just put the player up in the world.
So let's go dunk and then land on the platform, make your player fall in land.
So figure out what components we need to add to the player.
So when we push play, he falls and lands on the ground.
And now a little bit of bonus points here.
Figure out how we can ensure the player doesn't fall over, doesn't kind of slowly rotate and go up
and land on his face, which can happen if we do things a particular way.
So there's a challenge.
Pause video.
Take that one on.
Okay.
So there was a little bit of a clue in the sort of collider I'm going to be using on my player because
I was talking about having it fall over.
So I'm going to work directly on the prefab click my little arrow here.
Now I'm in the prefab, I'm going to click on Add Component and we'll add a capsule collider make sure
it's the two.
RD So you don't sit there for a while saying, why isn't this working?
Or do a little bit of editing on this?
And it's a big round ball that's a little bit fatter than what Ginger is.
I'm going to click on Edit Collider, grab the handles to the right and to the left and up and down.
Just a little bit of finessing to cover most of the player, right?
Happy with that?
That's good.
And click out of the edit.
Collider Next, what we need to do to have our player actually respond to gravity and physics is what
we need.
Add a rigid body, so rigid body to DH and our rigid body to DX.
Let's open that up.
Have a look at it.
Dynamic.
Yes, because we want him to be moving around unlike our world, which is going to be static.
And I'm going to find the constraints box in here because we've got a capsule collider and we've got
this rounded bottom.
There's the possibility that our player will slowly kind of go boom and roll onto his face because he's
standing on this rounded edge, the rounded edge I like better than having the square collider or the
rectangular collider because it doesn't have these sharp edges that can catch on things.
It's got this nice rounded edge, smooth edge makes it easier to bounce around and do stuff.
But I need to freeze the rotation on the Z axis so we can't accidentally roll forward onto our face.
So I think that's all I need to do.
Let's move back out of our player prefab click on play and our play should drop to the ground and they
bump and land nicely and not roll over fall flat on his face.
So there we go.
Next stage in getting our player ready for running around and jumping and platforming.
Good stuff.
I'll see you in the next lecture.