-
>> All right, everyone.
-
So this is part two of the Unity refresher.
-
Last class or last module, we talked about
creating scenes, I should say creating projects.
-
Creating scenes, importing 3D models,
adding lights, adding cameras.
-
We talked about creating materials in Unity.
-
We also talked about-- what
else did we talk about?
-
We talked about-- Oh, we also
talked about colliders and physics.
-
Okay. All right.
-
So, download the files for today.
-
You can look at the drills if you'd like to.
-
These drills are called Refresher Part II.
-
I have already added them into
my Unity Hub and loaded them.
-
This is exercise one.
-
Okay. This is my scene.
-
I have a red ball, okay,
and I have a yellow cube.
-
Okay. Now, if I put this in play
mode, not much is going to happen.
-
The reason why-- there we go.
-
Not much is going to happen and the reason
why is if I go back and I select this,
-
I don't have any physics
being applied to this, okay?
-
So it's not going to like move
or move around with gravity.
-
So I'm going to, just like we talked about
last class, I'm going to add physics,
-
which is going to be a rigid body.
-
Okay? If I go back, that should add physics,
which means it should fall with gravity.
-
Now, you'll notice it's falling
through the floor.
-
The reason why, and this goes back to what
we talked about, there's no collider on this.
-
So we're telling it to fall,
but it's not colliding.
-
You know, the way two objects collide in a game
system is they have to tell the program, "Hey,
-
these two things are knocking into each other."
-
The way that we can do that is
we are going to add a collider.
-
In this case, I'm going to
use a sphere collider, okay,
-
that goes around the entire
player, which is a ball.
-
What that should mean now is my
player should fall, hit the ground--
-
And it does.
-
Now I'm going to try to move
it, and I can't do that,
-
and that's because I need to add some code.
-
Okay. So in my scripts here, I'm going
to use this script called Player Move.
-
We'll talk about scripting later today.
-
But what I'm going to do is click
and add it to my character as such.
-
This allows my character to move.
-
I can play the game, I can move around.
-
What I'm going to do is hit this cube,
and I'm passing through the cube.
-
Okay. Why am I passing through the cube?
-
The answer is, well, if you click on the
cube, the cube does not have a collider on it.
-
So it's kind of like the same issue
that my red ball did, my player,
-
in that if both objects don't have
a collider, they don't collide.
-
So I'm going to add a collider.
-
I'm going to add a box collider to it.
-
All right.
-
And the results should be now that my
object or my player, which is the red ball,
-
should bounce off or collide
with the yellow cube.
-
And if I do that, boom, I'm doing that now.
-
Okay? Now that's great.
-
What I want to do though is I don't want
to get too deep into the code right now,
-
we'll cover that in a later video today.
-
But what I want to do is to-- I have this event,
okay, you guys remember events hopefully from p5
-
when you guys learned that in 327 and 427.
-
An event is something like a key press or like
a mouse press, right, so it's like, you know,
-
when you were writing code, it waited
for something to happen, right?
-
So I'm going to do this super quickly.
-
So if I go to p5.js, I can write some
code like this, function mouse pressed.
-
And what this will do is it waits for the event,
-
which in this case is a mouse
press and then it runs some code.
-
So I'm going to run this.
-
Nothing happens until I press
my kind and my, you know,
-
my mouse on my keyboard or mouse on my computer.
-
But then when I press it,
the code's been launched.
-
Okay? So what this is right here,
this is kind of the same thing.
-
The event here is though, not a
mouse press or a keyboard press.
-
Instead, what it is, it's waiting
for something to be triggered.
-
Okay. So the way I like to think
of it is kind of like a mouse trap.
-
All right.
-
So like I was saying-- sorry
for the pause there.
-
I was trying to get this slide going.
-
You got to think of a trigger kind of like
a mouse trap where it's like the action is,
-
you know, the thing coming down,
but the trigger is this, right?
-
The mouse steps on this and it
causes the trap to go, so to speak.
-
So what's happening is I
need to trigger this code.
-
Okay. I have some code written that says,
if-- I don't want to get too deep into this,
-
and we'll talk a little bit about this later.
-
This is a function, kind of
like the function I had here.
-
If I didn't close it or delete it, maybe I did.
-
Oops, maybe I did.
-
Anyway, in my example where I
was-- oh, I didn't close it.
-
All right.
-
So, this is a function, right?
-
And so is this.
-
And the function is basically saying, you
know, when this runs, you know, when--
-
"on trigger enter" means when a trigger
has been done, what's causing the trigger,
-
something to collide, colliding with
what, the other, do the following.
-
Okay? Which in this case is,
you know, causing this to occur.
-
Now, the way I'm going to get this to work,
I want that code, this line of code to work.
-
Now, normally it should show up in the console
here, but if I were to hit the play button--
-
Any day now.
-
Oops, there we go.
-
And I do this, it is colliding,
but it's not triggering,
-
even though it was supposed
to be a trigger code.
-
So what I'm going to do is the reason why
it's not working is if I go to my scene,
-
I need to have what's called a trigger.
-
Okay. What this says is if someone--
if something collides with the object,
-
it will trigger possibly some code.
-
Okay. Now the way I'm going to do that is, well,
-
we have this little option
here that says is trigger.
-
And I'm going to turn that on.
-
And let's hit play.
-
And what's going to happen
is now when I do this,
-
you can see I'm triggering
down here in my console.
-
Now, the other problem is this,
you might notice that, oops, well,
-
the collider is not actually
acting like a collider anymore.
-
I'm actually able to like run right
through this, like it, you know,
-
and that's not what I want either.
-
So the thing about this is like, well,
when you make a collider a trigger,
-
it no longer acts as a collider, okay?
-
I kind of feel like there should be a separate
component for this, but unfortunately,
-
at least currently, this is how it works.
-
So what I would say is like,
okay, I'm going to--
-
the way we can fix this problem is
I'm going to add another box collider.
-
Okay. So there's actually two box colliders,
one that's the trigger, one that's the collider.
-
So now one is going to stop the ball from going
-
through it while the other
is going to trigger the code.
-
And boom, okay.
-
So if I try to run through
this, that's not going to work.
-
Okay. So that's what a trigger is.
-
Trigger like it sounds like causes
possibly at least code to occur.
-
Okay? I'm going to go to
exercise, scene two here.
-
And I have a ball and I've actually already
added-- you know, it already has the rigid body
-
and the collider and I have this code
being applied to it that allows it to move.
-
Okay? And if I hit play--
-
What's going to happen is it's going to run
and it's going to hit that back wall there.
-
Okay? See what happened there?
-
And I want to write some code that if it
hits this, that's like the finish line.
-
I wanted to say in the console, you
would hit the finish line, right?
-
So I'm going to open up the code real quick,
-
which the one that's being applied
here is called Player Move Finish Line.
-
And what would happen is if I had something
like this, any time you collided with anything,
-
it would say you reached the finish line.
-
Okay. I'm going to save that.
-
Now, that's actually not what I want,
because like I'm going to show you why
-
that wouldn't necessarily
be something that we want.
-
Sorry, I'm waiting for my
code to save the update here.
-
Oops.
-
Uh-oh, what happened?
-
All right.
-
So the reason why I wouldn't want that to
happen is I'm going to add a cube to my scene.
-
Okay. Let me just make it real big.
-
And I'm just going to make it like black.
-
Okay. Let's also put-- let's say
that also has a trigger on it.
-
Okay. I'm just going to place it like here.
-
Okay. Now, there's two objects in my scene
and they both have triggers on it, right?
-
So if I were to run this, how does my
program know which one is the finish line?
-
Because they both have the trigger and then
what would happen is if I run into this--
-
oops, I don't know if I hit the trigger on it.
-
Oh, there it is.
-
So it's accidentally not,
you know, doing it correct.
-
Okay. Because the problem is
it's like the game can't--
-
or the game engine can't tell the
difference between this and this
-
because they all have triggers on it, right?
-
And all that my code basically says
is if you collide with something else
-
and then it has a trigger on
it, then you run this code.
-
So what I need to do instead is we
need to make it a little more precise.
-
And the way that-- an easy way to do that is
we can work with something called a game tag.
-
What a game tag is a game-- sorry about that.
-
Game tag allows you to organize elements
in your scene for coding purposes.
-
Okay. So what I'm going to do is if you
want to add a game tag, come up here,
-
and actually normally there will be some
already built into the scene, usually we'll have
-
like respawn and camera and
player, and stuff like that.
-
If you want to add one, I'm going
to go to where it says "add tag",
-
and I've already actually added a few.
-
I'm going to hit the plus sign here and
I'm going to add one that says-- oops.
-
Let me delete that.
-
Let me do it again.
-
And I'm going to call this finish
line, all lowercase, no spaces.
-
Okay. Now what I'm going to do is I'm
going to click on this and I'm going to set
-
that to that new finish line tag.
-
Okay. And then if I come into
here, what's actually happening,
-
hopefully, you guys remember if statements.
-
Remember in programming, if means, if whatever
it evaluates as true, then do the following.
-
Okay. So what this is saying basically is if
the object you collide with has a game tag
-
of the finish line, then do this.
-
So in other words, the finish line code,
you've reached the finish line will only run if
-
and only if it collides something--
-
collides with something that has a
game tag, a finish line applied to it.
-
So, because this one has that trigger,
but not the tag, it won't do that.
-
So let's just try that out.
-
If I hit the object here.
-
Oops. Oops.
-
I think I didn't turn on the trigger.
-
Yeah, this has to be a trigger.
-
Let's try that one more time.
-
Okay. See how that worked.
-
Now, I would actually fly
through it because once again,
-
because this is a trigger it
no longer acts as a collider.
-
So I'm going to add a collider to it.
-
Another thing that's kind of fun
is you technically don't even have
-
to have this be visible for the trigger to work.
-
You can turn off what's called
the mesh renderer.
-
The mesh render is what-- the mesh is the
visual part that the light bounces off of.
-
So if you turn it off, it
actually will make it invisible.
-
But the object is still there.
-
So what I'm going to do now, you can see
how we no longer see the finish line, right?
-
And then I'm going to go forward like this.
-
And you can see I'm hitting that wall there and
it's triggering even though we can't see it.
-
Okay? So game tags are useful.
-
We can use them to our advantage.
-
I would recommend that you-- you
know, the reason why you might want
-
to do it is maybe you want to
make a game tag for like enemy
-
and every time your character
hits an enemy, something happens
-
or every time your character
hits a tree, something happens,
-
or maybe every time your character
picks up an apple or whatever it is,
-
you can use game tags to kind of work with that.
-
Okay? Another thing we can
do-- so I have this scene here.
-
And I already have this, you know, here's my
player and the player has spheres, you know,
-
it has a collider on it, it has gravity
applied, and it has a script being applied.
-
All of the elements here have colliders on them.
-
Okay. So if I were to play this game or put
this into, you know-- what should happen is--
-
Oops.
-
Oops. Sorry.
-
Give me one second.
-
Let's try that again.
-
I hadn't set up my file correctly.
-
Yours should be set up correctly though.
-
So what should happen here is you can see
-
that I'm bouncing off all of
these objects here, right?
-
Okay. Let's just say for the sake of
argument, I want to have these objects here,
-
but let's just say for the sake of
argument, I don't want to have--
-
you know, I don't want the sphere
to interact with the cubes.
-
Let's just say for some reason,
I'm allowed to pass
-
through those, but not through other objects.
-
Okay? So, and maybe other objects
might interact with the cubes.
-
So I don't want to just make it
from everyone, just my player.
-
What I can do, another way to
organize this is you can actually work
-
with something called layers.
-
Okay? What you can do is you
can organize things into layers,
-
and then you can actually use those layers
-
to tell the program what is
allowed to interact with what.
-
Okay. So, I'm going to add a layer here.
-
And when I do that, you're going
to see I get things like these.
-
And what I'm going to do is I'm going to type
in-- I'm going to make a layer called Player
-
and I'm going to make a layer called Squares
and I'm going to make a layer called Spikes.
-
Okay. So, I've made these
layers, I'm going to close this.
-
And then what I'm going to do is I'm going
to put the player into the Player layer.
-
I'm going to put the cubes into
the cube or the Square layer.
-
I guess I should have called
it cubes, but whatever.
-
And I'm going to put the spikes
into the Spikes layer, okay?
-
Now by default that, you know, they are
organized, that doesn't really do a lot.
-
You couldn't turn off everything in one
layer if you really wanted to do that.
-
There are ways of turning
off objects within a layer,
-
but that's not what I really
want to do right now.
-
But if I hit play, nothing's
going to happen yet.
-
So, give me a second.
-
All right.
-
So see how I don't-- see how I'm not hitting
those objects, but I am hitting those.
-
Now-- actually, once again.
-
All right.
-
So, you can see that I did put them in layers,
but I'm still bumping in the things, right?
-
I don't want my player to hit these
objects, anything that's in either
-
of the Spikes or the Squares layer.
-
Okay? So what I can actually do is I'm going
to go to the project settings as a whole.
-
Remember, this is where we play
it around with the gravity.
-
I'm going to go under physics.
-
And under physics, this is where gravity was.
-
There's also this thing here
called Layer Collision Matrix.
-
And it might seem a little confusing.
-
But what it's doing is it's basically saying
what layers interact with what other layers.
-
Okay. The reason why doing this can help
is the more that your computer has to think
-
about all the things going on in your world,
the slower your world is going to run.
-
If your game doesn't have to worry
if two objects are colliding or not,
-
or anything from one layer is going to
collide with anything from another layer,
-
it makes the job of the game a little easier
and therefore it's going to run a little faster.
-
So the only thing I'm going to
worry about is here is this player.
-
See how player is set to Spikes,
Squares, UI, blah, blah, blah, blah.
-
What this is saying is, does the
player or anything on the Player layer,
-
which in this case is my red ball, is it going
to collide with things on the spike, square,
-
UI, water, player, and then default?
-
Okay. I don't want the player to
interact with the Spike layer.
-
And I don't want it to interact
with the Squares layer.
-
Okay. It will interact with
all of the other layers here.
-
Okay. So that means that it should interact
with my orange rings because although--
-
the reason why I should collide
with them is they're set
-
to the default layer, not
to one of these layers.
-
And so far, my Player layer should
be able to collide with this,
-
this, this, this, and this, and this.
-
Okay. So let's try that again.
-
Now, I'm going to go into game mode here.
-
And what's going to happen is you can see
I'm not going to hit any of those objects
-
but I will bounce off of these
because those were in default.
-
Okay. Now if I decided later on I didn't
want my player to interact with these.
-
Let's take the rings and put
them onto the Squares layer.
-
I think both of them got that.
-
No, let's send them both to Square.
-
Now, what should happen is my player-- or my
character shouldn't interact with those either
-
because it doesn't interact with
anything on the Squares layer.
-
All right.
-
See that, see how I passed right through that.
-
Okay. The last thing I'm going to talk
about, let's kind of put everything together.
-
Okay.
-
All right.
-
I have this player.
-
And if I click on this, it
already has the, you know,
-
rigid body applied and it
has this script applied.
-
Okay. And I'm going to run
the script real quick.
-
And the script essentially--
-
It sets up a variable.
-
Okay. Hopefully, you guys remember
variables from like p5 and all that.
-
And what it basically says is that
if you collide with another object
-
and that object has a game tag of ring, turn
off the other object, ring equals ring plus one.
-
So, you know, you have a variable
called ring, it was set to zero,
-
then it's going to be one, then two, then three.
-
Okay. And then it's going to say,
you have collected a ring, you know,
-
this is going to print the console.
-
So let's try this.
-
I have this ring right here and it
actually already has a collider applied
-
to it and it has a trigger.
-
Okay? What I'm going to do is I'm
going to create a game tag called Ring.
-
I keep doing that.
-
Let's do it again.
-
Plus sign, ring, okay.
-
And then I'm going to select
this and set this to ring.
-
And what should happen is when I-- you
know, when my character rolls, it should--
-
make that disappear.
-
And then I get a little message.
-
Now that's one ring, right?
-
Now, what if I wanted to
add multiple rings to this?
-
I could-- what I could do is I could add another
object, add another material, add another box,
-
the lighter, you know, do this, add,
set, you know, that that's a lot of work.
-
Okay? There's another way we can
kind of save ourselves a little bit
-
of time and it's called a prefab.
-
Okay? Prefab is short for prefabrication.
-
And what it means is that you can take any
object in Unity and make it kind of like sort
-
of like a finished model and you can
duplicate that object really quickly.
-
Okay. So what I'm going to
do is I want to take--
-
I'm going to get rid of this object,
which is the second ring here.
-
I'm just going to delete it.
-
And what I'm going to do to make this a
prefab, I already have a menu called pre--
-
or sorry, a folder called prefab, it doesn't--
-
you don't have to have this,
but I just think it's easier.
-
And what I'm going to do is literally drag it
into this folder, this project folder here.
-
Okay. And when I do that,
it's going to make a prefab.
-
Okay. You can tell it's a prefab because
if you look down here, it says ring.prefab.
-
Okay. This is showing you what that ring has.
-
So it has a yellow material applied to it.
-
It has a box collider.
-
It has a trigger.
-
It has a tag of ring.
-
Okay. And it tells you who's the
original parent, which is ring.
-
What I'm going to do is from this prefab,
-
I'm actually just going to
add a bunch of rings here.
-
So I'm just like clicking and
dragging and dragging this in.
-
And then I'm just going to
kind of rotate these--
-
as such. Maybe I'll put some back here.
-
Okay. So all of these rings
I've added to my scene,
-
they're all a prefab, they're
all a copy of this one.
-
Okay. And if I click on all of them, you'll
notice all of them have the same thing.
-
They have a box collider with the trigger,
and they have the tag of ring, box collider,
-
trigger, game tag a ring, box
collider, trigger, game tag a ring,
-
so on and so on and so on and so on.
-
Okay, I'm going to go and hit-- let's hit play.
-
All right.
-
And boom, boom, boom, boom, boom, boom, boom.
-
Okay. Now what's nice about prefabs are
if you were to ever change a prefab,
-
it would actually edit all of your copies.
-
So for instance, an easy
way to do this is I'm going
-
to take this prefab and I'm going to edit it.
-
I'm going to double-click, one, two.
-
And when you do that, you kind of get this kind
of blue, kind of, I call it the nether world,
-
the nether world where it's like,
it just kind of exists on its own.
-
And I can make any changes I want,
-
and any changes I make are going
to affect all of the copies.
-
So for instance, maybe I'm going to
change the material from yellow to red.
-
I'm just going to drag the red material
and you can see that they're updated.
-
Okay. Maybe I'm going to take
this model and, you know, maybe--
-
I don't think I can edit it from here, but
I'm going to use one of the existing ones.
-
So I'm actually going to switch
out the mesh for another mesh.
-
Maybe not.
-
I'm going to use a capsule.
-
So I'm using one of the existing meshes here.
-
All right.
-
And then I'm going to get out of this
by hitting this little back button.
-
And when I do that, see how
everything automatically changed.
-
So it's like I was able to swap out
all, not just the color but models.
-
I could have added or subtracted code or
anything I wanted to do, I could have done that.
-
It's a little confusing now.
-
So maybe I'll make this the yellow item.
-
And I'm going to hit Play.
-
And there you go.
-
All right.
-
So hopefully, you see why prefabs are helpful.
-
It's really good when you have big, more
complicated worlds to use prefabs so that
-
if you need to make changes,
you can kind of quickly do that.
-
Okay. In the next video, I'm going to
talk about programming, okay, using C#.