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
It’s time to see some feedback from our code. Let’s get Mike the Frog flying!
Unity 6 Documentation
Resources
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
Welcome back.
0:01
It's time to see some impact in our game
from the code that we write.
0:02
If you remember, all of the physics
affecting our player is coming
0:06
from this Rigidbody2D component.
0:10
So in order to affect this component
in our code,
0:12
we need to get a reference to it.
0:15
There are two common ways
to do this in Unity:
0:17
doing it directly in our code,
and assigning it in the Unity editor.
0:20
We'll learn and use
both multiple times in this course,
0:24
but we're going to start with
the code approach.
0:27
So first, we need a variable
0:30
that will eventually store
our Rigidbody component.
0:31
To do this,
we need to supply it three things:
0:35
the access modifier, the data type,
and the name that we want to give it.
0:38
When an access modifier is not supplied, it
defaults to private.
0:43
So technically, we don't need this,
0:46
but I to be as descriptive as possible
in my code, so I always add it.
0:48
That's up to you though.
0:52
So I'll write private.
0:54
Now the type we need is Rigidbody2D.
0:56
Again, ensure you're using the 2D type,
0:58
and notice
the B in body is not capitalized.
1:01
This still throws me off, but this is
why IntelliSense is a lifesaver.
1:04
Now for the name.
1:09
This one is a little bit
tricky compared to others.
1:10
I see a lot of people name it
RB, theRB, etc.
1:13
The reason it's tricky is the word
rigidbody in all lowercase
1:18
is already being used in the Unity
engine, though now it's deprecated,
1:21
you'll get some warnings
if you spell it out just that.
1:26
I personally to name it
rigidBody in camel case,
1:29
so with a lowercase r and an uppercase b.
1:32
Again, you can name
this whatever makes the most sense to you,
1:35
but I to be descriptive.
1:38
Now, as I mentioned
1:41
before, the Start
method is a great place to set things up,
1:42
so let's
assign our value to this variable there.
1:45
We do this by writing our variable name
and equals.
1:48
Unity has a handy method
for grabbing a component
1:53
that is present on the same game object
as this script.
1:55
We write GetComponent.
1:59
This method requires a type
so it knows exactly what it's looking for.
2:02
So let's write Rigidbody2d again
inside of these angle brackets.
2:06
We can simply copy
and paste it from up here,
2:10
and then close it off.
2:13
Now we have the ability
2:17
to manipulate this Rigidbody
component in our script.
2:18
So down at our conditional,
let's do just that.
2:22
I'll remove this log here.
2:26
And we'll type our
rigidBody variable and a dot.
2:29
There are a lot of built-in methods
for every component,
2:34
but the one we're looking for
right now is AddForce.
2:37
This will add a singular force
to our player and not a continuous one,
2:40
which is perfect,
2:44
because we just want a little bump
of flight on each button press.
2:45
There's a few variants to this
method as well, including X and Y.
2:49
Each only affect that axis,
leaving the other untouched.
2:52
This Y method is exactly what we want,
as we only want our player
2:56
to go up and down
and not move side to side.
3:00
If we hover over this method, we'll see it
requires two arguments.
3:04
The first being a float
for the force amount, and the second
3:08
a ForceMode2D type.
3:11
So let's try 20f for the force amount.
3:13
The F just indicates it's a float
and not another number type.
3:17
Let's put in our comma, and you'll see
the IntelliSense is giving us
3:21
two options of a ForceMode,
force and impulse.
3:24
The one that we want is impulse,
so let's select that.
3:28
This adds an instant force
to the Rigidbody using its mass.
3:32
Alright,
3:36
let's save this and try it out in Unity.
3:37
Let it compile.
3:40
Then press play.
3:45
I'll hit space and...
3:47
whoa!
3:48
That's a bit too strong.
3:50
Say, wouldn't it be convenient
if we could change our force amount
3:52
right here in the editor editor
without having to constantly
3:56
go back to our code, save, compile, etc?
3:58
Yes, it would...
4:03
And yes, we can.
Let's do that.
4:04
Let's create another
4:11
private variable up top here of type float
4:12
and name it forceAmount.
4:15
We can give it a default value
of, let's say 5f.
4:18
In order to see this in the inspector,
4:23
we need to add
an attribute to this variable.
4:24
Now quick note, I see a lot of people
online just making these things public.
4:27
If you make a variable public,
it will show up in the inspector for you,
4:32
but declaring variables as public,
and in doing so, making them accessible
4:36
to other scripts, just so you can see it
in the Unity editor, in my opinion,
4:40
isn't best practice.
4:45
So, before the private keyword,
let's put in some square
4:47
brackets,
and inside we'll type SerializeField.
4:50
This allows
4:55
us to see and modify
this variable right in Unity
4:55
while keeping it private
and contained to just this script.
4:59
Now, down in our AddForce method,
we can use this new variable
5:03
instead of hardcoding an amount in.
5:06
Let's save and check it out.
5:11
Now we can see it with an edit
field on our Player script component.
5:16
Awesome!
5:20
Okay, let's try this again.
5:21
5 doesn't quite feel enough.
5:24
Let's bump it up a bit.
5:26
This seems better, but it's a bit floaty.
5:29
Let's stop play mode.
5:32
Now take note,
5:34
if you make changes to properties
on a component while in play mode,
5:35
they will go back to their original values
after you stop play mode.
5:38
So I usually stop, change things
5:42
and play again to save myself
from forgetting that they'll change back,
5:45
but whatever workflow you prefer
is what you should do of course.
5:48
I'm going to set this force amount to 7.
5:52
We'll tweak things this as we go,
I'm sure.
5:55
Let's also inspect the Rigidbody
and try changing the Gravity Scale
5:58
to 2.
6:02
That feels better to me, more snappy
you could say.
6:07
I noticed though,
that if the player is moving downward,
6:10
it takes more button presses
to get it to jump up again
6:13
because our force is fighting
this gravity pull
6:16
You can leave this in if you like it but I'd
like the controls to be even more snappy
6:20
and in the player's favor.
Let's go back to the script
6:24
We can remedy this
by zeroing out the current
6:29
Y velocity of our player
before we add our force,
6:32
giving it kind of a clean slate
each time we flap its wings.
6:36
So even if the player is being drugged
down by gravity, if we hit our button,
6:40
it'll cancel that out
and then apply our force.
6:43
It'll happen so quickly
you won't even notice the reset.
6:46
So right
6:50
above our AddForce line,
let's type rigidBody
6:50
and what we're looking for is
linearVelocity.
6:54
This also has an X and Y variant.
6:58
Let's use Y again.
7:01
And this is expecting a float value,
so let's assign it 0f.
7:02
Let's save this and give it a shot.
7:07
That's what I'm talking about.
7:15
The player now feels it has some weight
and the controls feel nice and responsive.
7:16
While we're here, let's prevent the player
from flying above the screen.
7:21
Now the easiest way to do
7:25
this would be to add a collider
at the very top of the screen.
7:26
So let's just duplicate our ground object.
7:30
It looks I forgot to rename this one.
7:33
So let's name this one to ground.
7:35
Then we can either right
click and select duplicate
7:38
or use the keyboard shortcut
of Command or Control D.
7:41
Let's rename it to Player Bounds
7:46
and get the Move tool with our shortcut W
7:51
and slide it
right up to the top of our camera's view.
7:54
5.5 looks the perfect spot for me.
8:01
I'll remove the sprite renderer component
since we don't need
8:05
to ever actually see this.
8:08
So to remove a component, you simply click
these three dots here on the right
8:09
and select Remove Component.
8:13
Nice!
8:17
Let's test it out.
8:18
Now it's confined to the screenview.
8:25
Nice job, GameDevs!
8:27
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