Welcome back, my fellow game developers.
In this video, we are taking our game to the next level.
When we run our game.
You'll notice that not only we walk around,
but if we try to leave our map,
there we go. We cannot.
I swear I'm still pressing the achy.
I'm still trying to go down, but it doesn't work.
I'm still trying to go out.
It doesn't work. So we are confined in this little space.
We do that using rigid bodies, using colliders.
And this is really nice because finally we can feel the weight of our game.
So even though we could walk,
we could see we can do anything,
but now we actually have weight to our movement.
We can feel the world around us.
With that said, let's not waste any more time.
And let's get started.
O k.
So currently, when we run our game,
we can move and all that's great.
But as you can see, when we try to hit the wall or something,
we just move bastard.
And it's as if there is nothing in this world.
It feels empty, it feels not real even though we are moving in it.
So how are we going to fix that?
We are going to use something called a collider.
Let me show you what a collider is in the Unity documentation,
as you can see, always referred in Unity documentation,
you can get there through Google.
It's super easy, super effective.
Let's see what we have description based class of colliders.
We have box, sphere, capsule,
a mesh collider, physical materials and rigid bodies.
Something that we will learn in just a bit of
an object with a collider needs to be moved.
During gameplay, you can attach a rigid body component
and we'll just get the rigid body a bit of patients,
you can see all the properties that we can have,
the bounds, the contact offset, the material.
We have public methods such as re-cast closest point.
We have messages which are extremely important and we'll be using
a lot of Trigger Enter on trigger exit on Trigger Stay.
You can read the descriptions right here of what each method, what each property,
what each message means,
what it does, all the inheritance,
what we can use, the compare tag will be very important.
You can see that we have a lot.
You don't have to understand every single one of them.
It's just nice to know that you can,
if you have something in mind to implement,
you can always go into Unit documentations and see if what
you're looking for is available right here.
So with that said,
let's go back in here and let's go ahead and add
a box collider to one of these walls right here.
Let's start with the bottom one.
So the way we do this is we are going to right-click in here.
Or let's go to the background.
Let's add a component in here.
We're going to make it a box collider 2D,
because we are working into this box collider is 3D.
This one is 2D.
We click on it.
You can see that it fills the entire room immediately,
but we don't want that.
We actually want to edit this,
or we can click on the Edit right here.
You can see we can make it a trigger.
We can set an offset,
we can set the size,
so we can make this smaller on the y-axis,
or we can simply click on these three connected dots
right here and just move it manually.
So there you go.
So now we have this collider right here on
the wall or the bottom or whatever you wanna call it, we can unclick.
This will run the game.
And now our player,
when he moves down,
oh, well, what's happening?
The player still hitting the wall and he is moving past. Why is that?
Well, because we don't have a collider on our layer.
So this is going to be your challenge.
Your challenge is to add a collider to the player.
So at a collider,
a capsule, it doesn't have to be a capsule.
It depends on the size and the frame of your player.
The one that true personally got,
or if you're using the same one as I am,
make sure it's only player parent object, not the body.
And change the size of the collider to the appropriate size of your player.
So with these instructions,
pause the video right now and go do the challenge.
Okay, welcome back.
So how did you get on with that?
We are going to add a capsule collider 2D.
There we go.
We have the capsule, capsule shell and not even sure how it's pronounced.
So we click on here.
You can see the little green line in here.
So we are going to take down.
There we go and make it just about the size of our layer.
Now, let's see if that works.
If we run our game.
Now when we go down walk,
we are still moving through the object.
Why is it not working?
I should have probably warned you about this.
Now we are going to add a Rigid Body 2D.
And now when we run the game,
we shall see that we hit the wall.
But I didn't press anything.
If I move up and let go, we fall down.
Why is that?
What is happening?
Well, because when you add a rigid body,
you also add gravity.
Let's delve a little bit deeper into what a rigid body is.
So it control of an object's position through physics simulation.
So when you add a rigid body to a game object,
you add physics to it.
Rigid body object will be pulled downwards by gravity without adding any additional code.
And we can also apply our own forces to them,
which I mean by them, it's the rigid body.
You can also go into unit is documentations and delve deeper into what rigid body is.
And in fact, we are going to need to delve into it.
So I'm going to go in here.
Rigid body unity docs.
There we go.
We see the docs Unity 3D.
And you can see the description, the properties,
the public methods and so on and so forth.
So let's go back and again,
we can see right here we can change the mass, the angular drag.
We can change the way the collision is detected, discrete, continuous,
but it takes a bit more processing power,
the sleep mode, the constraints,
we're actually going to use them in just a bit.
You'll see why.
And we can change this from dynamic two kinematic, static.
And each one of those are explained right here.
But the main difference,
whereas it explained somewhere in here,
I'm not sure where you can read about each one to delve deeper into it,
but know that dynamic means to be CAN affected by outside forces.
Kinematic means it doesn't get affected.
And static means is just like a big rock.
It doesn't move at all.
You can see right here the gravity scale.
Let's set it to 0, save that, run the game.
And now you'll notice that we're not fall anymore.
So now when we go down,
you can see that we can no longer go under this.
But what, what, what happened?
And you can see that we move around,
we need to add a box collider.
But what happened in here and what's the, this jittering?
So first thing we'll fix as the flipping things.
So this is because we are rotating.
You can see right here,
it's actually because the physics has calculated that
we should rotate it because of the capsule right here.
So we'll freeze the rotation around the z-axis while Z,
because that show you if we click on the rotate tool up here or simply hit the Iike.
We can see when we rotate,
it's the z-axis that rotates us and we can clearly see it when we go out from 2D,
you can click on this to the right here, we go out.
You'll notice that the z-axis, and you can see it.
There we go.
It's around the z-axis that we are rotating.
So we'll stop the to the,
we'll set this to 0,
will freeze on the Z.
And now when we run our game,
we go down. And there we go.
We no longer flip if we move from left to right,
but we still have this jittering.
And what is this jittering?
Where is it coming from?
Well, it's a matter of code.
We can see right here that we are moving the player using his transform, that position.
So we have two colliding forces.
We have the transformed position trying to go through the swallow,
but we have the physics from the both colliders trying to push the player away.
So this is creating this jittering.
How do we fix this?
Well, we are going to start moving the player,
not using his transform position,
but using his velocity.
And velocity is available on the rigid body.
If you find that somewhere right here, I'm not sure what it is.
Let's see.
Velocity.
There you go.
We can add velocity in here,
angular velocity, the normal velocity.
We can change it right here.
So let's do that.
If we go and you know what,
let's add it up here.
We'll create a serialized field.
This will be a variable of type rigid body 2D,
and we'll just call it the player rigid body.
Save that. Oh, and one more thing you've noticed,
and I forgot to tell you this.
It's the way we name our variable.
The first word should always have the first letter not capitalized,
while all subsequent words will be capitalized.
Why is that?
Let's take an example from here.
You'll notice that player is not capitalized.
Rigid body is by the B for the body is not.
So if we save that,
we'll look in the inspector.
You'll notice right here, the variable,
the way it's written will be player rigid body with rigid body as a single word.
If we go back, we make this be capitalized.
We save that.
We go back in the inspector,
you'll notice that now the B is capitalized and it's a separate word.
So every time we capitalize one of the words, it becomes separate.
But we don't want that.
We want it as a single one.
You can see the same example and the movement speed.
When we go back, there we go.
So we've just created a reference to the rigid body on the player,
but we haven't added it because you can see right now that the player rigid body,
and there's just not how we do that.
We simply click on the rigid body right here and we drag it up.
So now you can see,
as the name suggests,
it's the rigid body on the dune layer.
We save that back in our game.
And here we are going to use instead of the transform position delta and all of that,
I'm just going to comment this outlet on, we'll delete it,
but I just want to have this all in front of
you so you know the process of what's happening.
So now we take the players rigid body,
access the velocity on it.
And it's going to be equal to the movement input times the movement speed.
And because we are using the velocity,
this will not require any Time.deltaTime.
We save that.
We go back in here.
Let's move this guy up a bit.
So now when we run our game, We move around.
And if we try to move through the wall, there you go.
You can see that we no longer have the jittering
because we have the two physics engines working.
Well, not the 2, 1 physics engine working on
the both libraries because of the rigid body.
And finally, we'll just add a couple of more colliders to the background.
So I'm going to add a box collider 2D.
I'm going to make this a bit smaller.
And now it's never recommended that you add multiple box gliders on the same game object,
but for now we'll make an exception.
This will rarely happen.
So there you go.
Let's add a box collider like that.
Let's add another one.
And this one will be to the side.
So we don't go out,
we'll just make it a little bit bigger.
And let's add the last box collider,
and this one will be the one on the other side.
There we go. We save all of that.
We run our game.
And now we walk down.
We can't walk to the left, we can't.
We walked to the right, we can't.
And if we try to move up,
we also can't, but there is a slight problem.
You can see that we still have space in here to walk up to.
So what I'm going to do is I'm going to just make
the players collider a little bit smaller on the top.
So let's just make it around here.
We can even maybe change this from a capsule to something else and making it like that.
Oops.
Where is it?
We change the position,
so we move it on the X. Yep, there we go.
We run our game.
And we can see that we kind of move up.
We still have this bit of space.
This isn't a huge problem.
We can even make B instead of a capsule,
make a box collider or something else.
But you can see that it all works of fine and dandy with that said,
make sure to commit your changes.
And in the next video will be aiming our gun around using
the direction or where our mouse is pointing because currently,
just have this gun is not doing much.
I'll see you in the next one.