>> Hello, welcome to our Unity 101 videos. I'm Abdulla.
In our last video, we talked about one of
the most exciting subjects in game development,
which is taking player input.
In this video, we're going to talk about
another exciting topic in game development,
which is Physics and Collisions. Let's get started.
The first thing to know about physics within Unity,
is that you don't really need to use physics.
You can just control everything using logic with C#.
But if you want to add a layer of realism to
your game or unpredictability,
then physics are the way to go.
To use physics within Unity,
you need two things.
You need a Rigidbody component added to your object,
and you need a collider to match it.
When we created that cube previously,
you'll notice that there's already
a box collider applied to the GameObject.
This component will be responsible
to sending messages to your script,
like on collision enter for example.
If you click on the edit collider button,
you can actually control
how big that collider is relative to the GameObject.
But for the GameObject to have physical properties,
we need the Rigidbody component.
Let's press, "Add Component",
search for "Rigidbody" and add "Rigidbody".
That will give us so many options,
like specifying the mass,
the drag, the angular drag,
does it react to gravity?
After adding the Rigidbody component and pressing "Play",
you'll notice that the GameObject will just fall because
the UseGravity Boolean is set to true in the Rigidbody component.
Let's add another GameObject so that the cube can interact with.
Let's create a 3D object,
and let's choose plane,
and just move it down a little bit.
What I want to show here is how
a collider can interact with another collider.
As we can see here,
that the plane already has a collider component inside of it.
We don't need a Rigidbody component in
the plane because we're not really moving the plane.
If you press "Play", we can see that the plane
stops the cube from falling any further.
If we press "W", we can see that the cube is spinning,
following the script that we created in our last video.
How about we create a script that bounces
the cube whenever it touches the plane.
Let's go to "Add Component",
type bouncing, and create a new script.
Now lets edit it in Visual Studio.
You can right-click anywhere,
choose "Implemented Unity Messages",
and search here for "OnCollisionEnter".
This unity message or method is
triggered once if a collider intersects with another.
Now let's print something very simple to
the debug console just to make
sure that the collision is actually happening.
I'm going to print landed on plane.
If you go back to the editor and press "Play",
you'll see the messages being printed in the console.
Now I want to set it up so that the cube will bounce
up whenever it collides with the plane.
To start with that, let's create a public Rigidbody called RB.
This will represent the Rigidbody
that's attached to the same GameObject.
I'll also need a Vector3
representing the force that I'm going to apply to the cube.
In "OnCollisionEnter", I'm going to write rb.AddForce.
This is a method that will take a Vector3 and apply
that vector as a force to the GameObject with the Rigidbody.
Now let's go back to the editor and press "Play".
We can see an error down there.
It seems like we probably need to assign
the RB variable in the bouncing script. Let's do that.
Since we set RB as a public variable,
we can set the value of it from the inspector.
Let's drag cube into that.
The editor is going to look for
a Rigidbody within that GameObject that we
just dragged in and assign RB to that Rigidbody.
Now let's assign the force we want to apply.
Since we want it to jump up,
let's assign it to the y-axis.
Let's change it to something like 500,
and let's press "Play".
The cube is bouncing all right.
But it's flipping all over the place because
the physics engine is not really that predictable.
How can we constraint the movement of a Rigidbody?
Obviously, from the constraint
Booleans within the actual component.
Let's freeze the position of the X and Y axis,
as well as freeze rotation around the X,
Y, and Z axes, and press "Play" again.
Now this is a much more predictable behavior.
Just bouncing up and down. Perfect.
So what if I wanted the cube to only bounce
when hitting a certain object and not others?
In this case, we want to use something called tags.
Let's click on "Plane", and then "Specter".
Let's choose tags from the top,
the dropdown menu right there,
and choose "Add Tags".
Press the "Plus" sign,
and let's write floor and "Save".
Now let's go back to the hierarchy and choose the plane.
Now, in the drop-down menu we can
see the tag floor, and let's choose that.
Now plane has a tag floor.
Now let's go back to "Visual Studio" and "OnCollisionEnter",
let's write if collision.gameObject,
referencing to the object that we're colliding
with, .tag, equal equal floor.
That means that the AddForce function that we have
here is not going to be
executed unless we interact with the floor.
Now back to Unity.
Let's duplicate the plane here.
Let's move it around just a little bit to the right,
better zoom out a little bit to the top.
Great. Just position the camera a little bit better.
Now let's untag that new floor that we created so that
it wouldn't bounce if the cube
interacted with it, and press "Play".
Whoa, look at that.
Wow, that was a perfect landing.
Did you have fun playing with physics?
I always have a blast playing with it.
Just changing parameters, playing around,
and just seeing unpredictable behavior that sometimes
can turn into really interesting playing mechanics.
Just play around with it. In our next video,
we're going to talk about how to solve
problems in your code if they happened.
How do you figure out which line of code
is actually the problem?
We're going to talk about debugging,
so I hope to see you there.
[MUSIC]