In this lecture, I'm going to be adding a second game object.
And also we're going to be playing around with colliders and rigid bodies so objects can bump into each
other and use the unity physics system to apply force to one another.
So let's jump in and get started.
Let's create ourselves something to bump into.
So a little micro challenge for you here.
Add a new sprite into our scene, which is a square or a circle.
Okay.
So over in our hierarchy, right click to the object sprites.
I'm going to add a circle, I think, and then gives a chance to name it.
I'll leave it as circle for the moment.
I'm not exactly sure what it's going to be yet.
I'm going to click on that circle hit.
Just move it over to the side.
Well, here we can also play around with the scaling of it.
So I might do that, hit R on the keyboard and just scale it up a little bit around about there.
You know what?
I'm just going to type in to tab two.
Tab two.
When I say tab, I'm not typing tab, I'm just hitting tab to move to the next cell so that I can type
in 2 to 2.
There we go.
We've got a two sized circle to bump into.
I'm also going to change the color from within Sprite Renderer.
You see color as a field.
Click on the white space in there and change the color circle thing around the outside and then circle
thing on the inside.
It's kind of a yellowy orange color.
There we go.
So I'm going to click on play.
We'll see what behavior we've got here at the moment.
If I turn my vehicle towards the circle, drive over it or towards it, I just kind of drive past it.
Nothing happens.
The circle doesn't move.
There's no bumping into going on here.
So what do we need to do to get these things to bump?
At the moment, all we've got is a Sprite renderer as the component on these game objects that says
it looks a certain way.
It's an image.
Basically what we need to do is add to things.
One of the things needs to go on both of these objects and the other thing needs to go and only one
object.
Okay, so what are these two things?
First of all, we need to add colliders.
So we go on to our player, click on our player, then find down the bottom in the inspector, add component.
I'm going to search for Collider and we've got a whole bunch of options in here.
Now, pay close attention to the fact that some of them say 2D and some of them don't say 2D.
The ones that don't say 2D is when we're working with a three dimensional object.
In other words, something that's not a Sprite.
But when we're working with Sprite, as we are here, we want to be adding things that say 2D down the
end.
So what have I got here?
Well, it's a capsule, so I'm going to look for a capsule.
Collider two d click on that and then scroll right in.
So you can see this.
You can see we've got our capsule Collider 2D component.
And what I'll do just for a moment is to turn off my Sprite renderer so that we don't have our image
anymore.
And you can see we've got this green line around the outside.
Now, the player never sees the green line, but Unity knows that this green line is collision.
It's an invisible force field that we can have things touch.
And when they touch, we can say what the behavior will be.
For example, they they bump off and go in a different direction or they can't pass through.
So this is when we have a collider.
We're also going to talk about triggers in the next lecture.
That's something slightly different where you say some things can pass through this collision area,
but when it does, we want to send a message.
We want to know that it passed through it.
You know, you've you've entered the force field and something happens or you've exited the force field
or you're still inside the force field.
So there's a lot of things we can do in relation to this collision.
So I'll turn Sprite Renderer back on and then we'll jump over to our circle and we'll do the same thing.
So little micro challenge for you again.
Pause the video and add the appropriate sort of collider to your circle or to your square.
If you added a square instead of a circle.
Okay.
So we click on Circle Add component.
My search that I put in before is still there.
So.
Collider And because it's a circle, I'm going to add a circle collider to DX.
If you added a square, then you would add a box collider to DX.
And once again it should conform nicely to the shape of the object that we have in there, which is
really handy.
So you can see there we go, we've got our collision.
Okay, so we have collision now on the outside of our objects.
That means that unity knows, okay, what is going to bump into what.
But there's one last step we need to do, and that is to add to at least one of these objects needs
a rigid body.
What is a rigid body?
A rigid body is a way of saying this thing has substance.
And it's the thing that the unity physics engine understands that it's going to move around and it's
going to be able to bump into and collide and have physics interactions.
So let's scroll down, we'll click on our player, scroll down to add component and we'll search for
rigid body, fine rigid body 2D and add that this now says the player.
Our cruising MC drive can interact with things using the physics system in unity and if we click on
play, we'll see some behavior here.
That's quite interesting.
Our object now just automatically drops off the screen.
Why is that?
Well, that's because we've got gravity on.
So if we set gravity to zero, that means that this object is not going to have any gravity applied
to it and it's not going to drop off the screen.
Why would we want gravity?
Well, if we're making a side scrolling platform, we want to be able to jump and drop and jump and
drop.
And gravity is quite useful and quite important.
But for us, we're making a top down driving game and there's no gravity that we can see.
So now when I click on play, we see it doesn't go anywhere.
And if I drive my vehicle, drive, drive, drive and bump, it bounces into this and kind of gets flung
out of the way.
It's not actually moving the circle.
We'll get to that in a second.
But I can actually have this work with the collision.
But it's a bit odd because I bounce into this thing and it's just staying there and not moving.
That could be valid if you've got, say, a car and then you've got like a street post or a building
or something that you don't want to have.
Move like a car is not going to go and move a building.
The building is going to stand there and say, No, you didn't hurt me at all.
But what if we wanted to move this object?
When we bumped into it, we wanted to have it respond a little bit.
Well, this is a little micro challenge for you.
Again, what can you add to our circle so that when the car bumps into it, it moves?
We can add a rigid body to this object to the circle.
So add component once again rigid body to and what do we need to do?
First, we need to change the gravity scale to zero.
That's important so it doesn't go dropping off the screen.
Now I'm going to click on play, turn my vehicle around and there's a little bit of a reaction.
It's moving a little bit when we bump into it.
It's not going flying off the screen and that's all fine.
But I'm finding it a little bit difficult to kind of push the object because I keep sliding off because
we've got this precise collision on the front of our objects, all very sliding, if you will.
It's not hitting it tightly.
What you could do, it's going to click play is click on our player, and then I'm just going to roll
up a lot of these components.
So if you click on the little arrow triangle thing here, I'm going to roll these up, roll out the
Sprite render.
It's still there.
It's just I'm not seeing all the details.
It's a way of hiding it and making your inspector more clean and tidy.
So roll up all of these.
Roll up all of these.
There we go.
It's a little bit tidier.
I can see what I've got.
And the Capsule Collider I'm going to click the little checkbox that says, Don't use this when we play
the game.
We're not using this anymore.
It's still there, but we're just not using it.
I'm going to add a component which will be a collider, see a box collider to DX.
Now, when I look at my player, you can see that it's more of a straight edge at the front.
So when I click on play, drive over to my circle, it's going to act a little bit more like a a thing
that can push a bit more of a bulldozer, if you will.
It's not going to give a very good effect when I do that.
Like you can see it's going to look odd for the player.
So I'm using this a little bit sparingly just for you and I now to see that that is another way that
we could push a thing.
Instead of having this capsule random thing at the front, we can have it as a square collider and also
showing you that you could change the collider.
Now what I could do is for my capsule collider, I could right click and remove component and we've
deleted that.
That's not there anymore.
I've just got this box collider on my player, so it's pretty easy to add and remove colliders as your
heart desires.
So just to recap, we've added colliders to our to game objects so that they have the ability to understand
when they're connected and touching and bumping into things.
And we've also added rigid bodies to both of them so that they both have the ability to respond to our
physics system.
They can be moved around, they can interact, they can bump and all those kind of good things.
So there we go.
A quick introduction to colliders and rigid bodies.