"Enhancing a Simple PHP Application" was retired on December 19, 2016. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Unity Basics!
You have completed Unity Basics!
Preview
Here we’ll introduce Unity's physics engine by adding a Rigidbody and Colliders to our game objects.
Unity 6 Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, currently our
game object is just static in our scene,
0:01
so what we need, basically, is gravity.
0:04
Luckily for us, Unity has a built-in
physics engine which makes our jobs
0:07
much easier.
0:11
So to add a new component to an object,
we select it in the hierarchy,
0:13
and in the inspector
we can click add component.
0:17
This opens
a very intimidating select menu,
0:21
but I encourage you to explore
all of the options here.
0:24
I always use the search option here,
0:27
but that's in part
because I know what I'm looking for.
0:29
When just starting out, I would typically
look through the options, or go to Google
0:32
if I was looking for something specific
and I didn't know the name of it.
0:36
So let's click in there and start typing
rigid body.
0:40
Here we see a Rigidbody
in a Rigidbody2D.
0:44
We're making a 2D game,
so we definitely want the 2D option.
0:48
The other is used for 3D applications.
0:51
And now it's seen in our inspector
as a component.
0:55
Easy as that!
0:57
I'm going to collapse this
0:59
sprite renderer since I'm zoomed in here,
but you don't need to do this.
1:00
The Rigidbody2D
component applies Unity's built-in
1:04
physics to the game object
that we place it on.
1:07
We can see by glancing
1:10
at this component's properties
that it has one named Gravity Scale.
1:11
If we hover over this label
we see a tooltip and it says how much
1:15
gravity affects this body. Perfect!
1:19
Let's click play and see what happens.
1:22
Well there it goes!
It definitely has gravity
1:25
now, but it fell straight
through the screen!
1:28
I'm going to separate my scene
and game view
1:31
for a moment to show you further,
but you don't have to do this.
1:33
If I zoom out with the scroll wheel
and click play again,
1:37
you'll see it's just falling forever
outside of our camera's view.
1:40
Again,
don't feel the need to follow me here.
1:43
I just want to quickly give you a visual
of what the camera is doing.
1:45
If I click this button that
1:49
reads 2D, it will change our scene
view to a 3D view.
1:51
This can make things a bit more difficult
when making a 2D game,
1:55
but it shows the camera situation
pretty well.
1:58
You can see here it's essentially
a view box, and anything within it
2:01
and in front of
it will be shown in our game.
2:04
So what we need next,
2:09
logically, is a ground
to catch our circle.
2:11
So let's create another game object.
2:14
Let's go to 2D Objects,
2:16
and Sprites again,
2:19
and this time let's create a square.
2:21
I'm going to right click the Transform
label and reset its position.
2:24
This is something
2:28
I typically do with new objects,
just to have kind of a clean slate.
2:28
We want it at the bottom,
2:32
so I'll select the Move tool with W
and drag it down there.
2:33
I want it to go past the camera view a bit
so that we don't see the background
2:37
beneath it.
2:41
Negative 5 looks perfect.
2:43
Now I'm going to get the scale tool with R
and drag out
2:46
the X scale of its transform
so that it covers the whole camera view.
2:49
You can go outside the camera lines
here too.
2:53
Okay, let's click play again
2:57
and see what happens.
2:58
Oops, it fell right through it!
3:02
What we want is for these two objects
to collide
3:05
with each other, and to achieve that,
we need another component.
3:07
You guessed it, a collider.
3:11
Collisions are one of the most important
aspects of game development.
3:14
If everything passed through everything
else, we wouldn't be able to make anything
3:17
very fun.
3:21
Players couldn't run on the ground, cars
couldn't crash into each other, and so on.
3:22
So let's click Add Component
and search for Collider.
3:27
We'll see a lot of options here,
but we're working with a square,
3:31
so the one we want is Box Collider 2D.
3:34
Again,
make sure you're using the 2D option.
3:37
Now it's hard to tell, but
3:41
it's actually been automatically applied
to the exact shape of our sprite.
3:42
If we enable this edit
collider button here and zoom in a bit,
3:46
we'll see four green squares
on each side of the object.
3:50
We can click and drag these to edit
our invisible collider shape.
3:53
This means we have the freedom
to have parts of our objects
3:57
pass right through things
and other parts detect collisions
4:00
I going to undo this though
because I just want the entire box covered.
4:04
There also the offset and size fields
4:08
here to adjust this collider shape
if we want to.
4:10
Okay, now
4:14
we need to add one to our circle as well
or it'll still pass right through it.
4:14
So let's select our player,
and I'm going to collapse these components
4:18
to free us up some space,
and then add a component.
4:21
Oh, how convenient,
our collider search is remembered,
4:25
and this time
we want a Circle Collider 2D.
4:28
Again,
4:32
it's applied it perfectly
to the edges of our sprite.
4:32
Also, real quick,
these colliders are perfectly forming
4:36
to these two shapes because
we're using a square with a box collider
4:38
and a circle with a circle collider,
and they play well together.
4:42
If we had a human shaped sprite
for example, we won't be getting this
4:46
perfect formation obviously,
but don't worry about that stuff yet.
4:49
Let's click play and see what happens.
4:53
Alright, we've got collisions!
4:57
I'd say this wraps up this game.
5:00
Thanks for watching everyone!
5:01
We'll see you next time.
5:03
Alright, kidding, of course.
5:04
This achievement may seem small,
but this is huge in the world of game
5:07
development.
5:10
We have physics and collisions
already working and it was as simple
5:11
as that to implement. Nice job.
5:14
Next, we need to get this circle doing
something besides, you know, just falling.
5:17
So in the next few videos, we're going
to be implementing the most crucial aspect
5:22
of creating a game. Capturing user input.
5:26
I'll see you there. you
5:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up