< Return to Video

Unity Triggers / Game Tags / Layers / Prefabs

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

more » « less
Video Language:
English (United States)
Duration:
30:01

English (United States) subtitles

Revisions