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
    
      
  Let’s learn how to detect when the player hits a pipe obstacle.
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, so the last bit of core logic
we need to implement
                      0:01
                    
                    
                      is handling the player
hitting a pipe and losing their run.
                      0:03
                    
                    
                      This means that in our code,
we need to detect
                      0:08
                    
                    
                      when the player collides with something.
                      0:10
                    
                    
                      But the player can also collide
with the ground and our invisible player
                      0:12
                    
                    
                      bounds up top,
                      0:15
                    
                    
                      so we need a way to make sure it's a pipe
that they're colliding with.
                      0:17
                    
                    
                      There are numerous ways to do this,
but the most simple way I know of
                      0:20
                    
                    
                      is using tags.
                      0:24
                    
                    
                      It's also not as taxing
on your performance as other options.
                      0:25
                    
                    
                      Tags in Unity are just labels
you can assign to game
                      0:29
                    
                    
                      objects
for easy identification and grouping.
                      0:32
                    
                    
                      If we select a game object
in our hierarchy and look just beneath
                      0:35
                    
                    
                      the name field up top, you'll see
a tag option with a drop down menu.
                      0:39
                    
                    
                      There are some default tags we can use,
but we have the ability to create our own
                      0:43
                    
                    
                      as well.
Let's do that by selecting Add Tag.
                      0:47
                    
                    
                      This opens a menu for tags and layers.
                      0:50
                    
                    
                      As mentioned, tags are just text labels
for identifying objects.
                      0:53
                    
                    
                      Sorting layers determine the order in
which sprites are rendered to the camera.
                      0:58
                    
                    
                      Layers are regarding physics
and rendering layers are used by cameras
                      1:02
                    
                    
                      and lights to determine what gets rendered
                      1:06
                    
                    
                      but we won't need to worry
about those in this course.
                      1:08
                    
                    
                      Under the empty tags
                      1:11
                    
                    
                      list, let's click this plus icon
to add a new one, name it Pipe,
                      1:12
                    
                    
                      and hit enter. Now similar to the input
button earlier, this is case sensitive,
                      1:16
                    
                    
                      so it needs to be spelled and capitalized
exactly the same in our code.
                      1:21
                    
                    
                      And don't worry, this didn't add
                      1:25
                    
                    
                      this new tag to our game object
that we had selected.
                      1:27
                    
                    
                      We could have assigned the player
tag to our player object,
                      1:30
                    
                    
                      but I wanted to show you that process
of creating a new tag
                      1:33
                    
                    
                      and the following process
of editing a prefab.
                      1:36
                    
                    
                      So as mentioned before,
in order to edit a prefab,
                      1:39
                    
                    
                      we find it in our project window
and double-click it.
                      1:42
                    
                    
                      This changes the hierarchy and inspector
windows to our prefab editor,
                      1:45
                    
                    
                      and now we can make changes to it
                      1:49
                    
                    
                      that will carry over to all future pipes
that we instantiate.
                      1:51
                    
                    
                      Let's select both of our individual pipe
children objects by holding
                      1:55
                    
                    
                      shift and clicking them.
                      1:59
                    
                    
                      We want the tags to be on these
and not the parent
                      2:01
                    
                    
                      because these individual pipe objects
have the colliders attached to them.
                      2:04
                    
                    
                      Now open the tag dropdown
and select our new pipe tag
                      2:08
                    
                    
                      Now to go back to the normal editor
we click this tiny little left
                      2:14
                    
                    
                      facing arrow at the top here.
Perfect!
                      2:18
                    
                    
                      Let's now open our Player script.
                      2:21
                    
                    
                      Unity has an awesome built-in
method for detecting collisions.
                      2:27
                    
                    
                      So beneath our Update method here,
but still inside our class,
                      2:30
                    
                    
                      let's start typing OnCollisionEnter2D.
                      2:34
                    
                    
                      Again,
make sure you select the 2D version.
                      2:38
                    
                    
                      This method will get called
                      2:42
                    
                    
                      every time our player collides
with another collider in the game.
                      2:43
                    
                    
                      It also automatically receives data
about the collision
                      2:47
                    
                    
                      and the other collider involved.
                      2:50
                    
                    
                      By default, Unity named this collision,
but typically you'll see people
                      2:52
                    
                    
                      rename it to other, which makes things
a bit more clear, so let's do that.
                      2:57
                    
                    
                      So we
need to ensure that this other collider
                      3:02
                    
                    
                      belongs to a game object
that has the pipe tag, right?
                      3:05
                    
                    
                      Right.
                      3:08
                    
                    
                      Sounds a good place
for a conditional statement.
                      3:09
                    
                    
                      We can do this by typing if other
                      3:13
                    
                    
                      dot game object, with that
lowercase g again. And now, instead
                      3:18
                    
                    
                      of the other collider, we're referencing
the game object it's attached to.
                      3:22
                    
                    
                      Now that we have the game object itself,
we can check its tag property.
                      3:27
                    
                    
                      So we can say .CompareTag.
                      3:31
                    
                    
                      If we hover over this,
it states returns true
                      3:36
                    
                    
                      if the game object has the given tag,
and it requires a string of the tag name.
                      3:39
                    
                    
                      So, within the parentheses, let's put our
quotes for the string and provide pipe.
                      3:45
                    
                    
                      Mine had a capital P,
make sure yours is just how you wrote it
                      3:50
                    
                    
                      when creating the tag.
                      3:53
                    
                    
                      In our block,
                      3:57
                    
                    
                      let's Debug.Log a message
letting us know it's working.
                      3:58
                    
                    
                      Save and head back to Unity to test.
                      4:06
                    
                    
                      If I immediately hit the floor,
                      4:15
                    
                    
                      we don't see a message,
but the moment I hit a pipe, we get it.
                      4:17
                    
                    
                      Perfect.
                      4:20
                    
                    
                      So we know we're ready
to add our losing logic.
                      4:21
                    
                    
                      Let's handle that in the next video.
                      4:24
                    
              
        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