Unity Beginner Tutorial - Part 4: Collisions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you want to learn how to make a video game in unity this tutorial should help you get familiar with collisions in the last video we set all our 3d pipes to spawn infinitely in random heights and directions all while moving towards our player now it's time to destroy our bird if it collides with an obstacle and add points for every collision the player can avoid in unity the first thing we'll do is select a game object to take a look at some colliders if we go down into the prefabs we created in the last video let's double left click on our bottom pipe prefab with our bottom pipe prefab open we can see two game objects that make up our 3d pipe the top and base if we select the top game object we can see in our list of components that it already has a capsule collider attached to it this is one of the colliders responsible for pushing our bird backwards if our player flies into the top of our pipe capsule colliders work great for objects with rounded sides unity included this by default because we selected a cylinder to make the top of our pipe to take a closer look we can temporarily turn off our mesh renderer and only view the capsule collider it's this green outline that you see here although these rounded sides are useful in games played in the first or third person our game only plays from left to right even though our objects are 3d the game is more like a 2d experience to better visualize this let's take a look at our object in the 2d view by clicking on this little 2d button up here in the top if we zoom out a little you can see the capsule collider juts out above the top of our pipe the player could hit this and die even if they clear the pipe you can probably imagine how frustrating that would be so let's go ahead and remove this capsule collider by left clicking on these three little dots and going down to remove component i'll left-click on that now let's take a look at all the colliders unity has by default to do this let's go down to the add component button left click and type the word collider in this search bar this filters out all available colliders in unity for our game let's select box collider so i'll left click to add that to our game object if i come out of 2d mode and turn my mesh renderer back on so i can see the pipe again you may notice that these corners jut out on the sides this box collider doesn't exactly fit our pipe's rounded shape well but because we only care about collisions on the left and top of our pipes this actually works perfectly if we go back to our 2d view we can see our top is now smoothed out and flush with what the player would expect if they hit it while we're here let's make this same adjustment to our base object so i'll left click on base go to my capsule collider remove it come out of 2d mode so to add a box collider to this game object let's left click on add component again left click on box collider and it automatically fits i'll switch back into 2d view one last time i'll select the top and the base by holding shift you can see this green outline is perfect it's flush with the sides of our pipe great next we need to tell unity to do something other than just push our bird backwards if the player collides with one of our pipe obstacles but before we get into any code let's take a look at something with our top game object selected again you might have noticed there are a couple drop downs here right below the name field one called tag and one called layer for this tutorial we'll keep everything on the default layer but let's change this tag if i left click on this untagged drop down you'll see that unity includes a few tags by default and at the very bottom it gives us the option to add our own tags let's left-click on that to create a new tag all we have to do is left-click on this little plus sign and name it and the name we'll give our tag is obstacle then click save now if we go to our top game object again and just left click to select it we can go over to the tag drop down and select our brand new obstacle tag that we just created let's go ahead and left click on that next let's go into our base object and do the exact same thing we'll go to our tag where it says untagged i'll left click and select obstacle by left clicking on it great now let's back out of our bottom pipe prefab to save it and select our top pipe prefab now with our top pipe prefab selected we can do the exact same steps a lot faster if we hold shift and left click on the top and base objects we can select both at the same time go over to our capsule colliders let's remove them by left clicking these three little dots and going to remove component then go down to add component and left click on box collider you'll see that both of them now have a box collider attached and before we back out let's go to this tag where it says untagged and select our obstacle tag that we created just a second ago and i'll back out to save it perfect now let's put our new colliders and tags to use to do this let's create a new c-sharp script that determines what happens if our player collides with something we'll name it player collide manager so i'll go down to my scripts right click go up to create and left click on c sharp script and we'll name it player collide manager just like that while we're here let's left click on our bird player and attach this player collide manager script to it by left clicking and dragging it right under our player movement script perfect next let's open our player collide manager script by double left clicking on it down here in our project folder inside our new player collide manager script i'll first clean up our usings by removing our system.collections and system.collections.generic namespaces and i will remove the start and update methods as well next let's make sure we click inside our player collide manager class and i'll create a new method by typing void and then space and then we'll type in on collision enter because on collision enter is a special method in unity visual studio might recognize what you're doing and let you auto complete the method at this point you can still type everything out but pressing the enter key will let you work a little faster so with on collision enter highlighted i'll go ahead and hit the enter button on my keyboard unlike the other methods we've seen so far you might have noticed this method has a variable inside its parentheses this is called a parameter parameters just allow methods to take in information whenever they're called we've actually passed information into methods similar to this in our other scripts for example if i type debug.log and hover over the log here you can see that thedebug.log takes in an object parameter named message instead of object the oncollisionenter method takes in a collision parameter this means that anytime unity calls the oncollisionenter method it's required to include collision information and you might have guessed unity's physics system automatically calls this method anytime two colliders attempt to enter one another you may remember our bird player game object included a sphere collider by default to check if our bird player game object collided with an obstacle let's create a new if statement now inside our if statements parentheses let's take this collision variable by double left clicking on it and hitting control or command c on your keyboard and control or command v to paste it then type dot game object this is going to take our collision variable information and figure out which game object triggered the collision next let's type dot compare tag then open and close parentheses this will let us look up the tag assigned to the object we collided into inside these parentheses we need to add quotation marks and type out the obstacle tag that we just created in unity be sure to type it out exactly how we created it within unity next let's select our debug.log and hit controller command x to cut it and control or command v to paste it then inside the parentheses let's add quotation marks and type the words game over before we head back into unity let's also destroy our birds so it's visually clear what just happened to do that just type the word destroy and then open close parentheses and end it with a semicolon inside these parentheses we can get the game object that this player collide manager script is attached to by simply typing the word game object in our case we're destroying our player object finally i'll just clean this up a bit by removing the space and hit controller command s to save my script and then let's head back into unity now when we click play and test out our game if we hit one of these pipes our bird should be destroyed just like that and you can see it's no longer in our hierarchy and in our console it says game over while we're still in unity we can increase the difficulty a bit by making our ground also kill the player and all we have to do is left-click to select the ground object we created in part one and go over and select the tag of obstacle now if we click play one last time and just let our bird drop you'll see it gets destroyed on impact and then you can see this game over message appears in our console great next let's create something to add points if we fly over or under a pipe without hitting it to do this we'll set up a collider to act as a trigger i'll left click on our project again go into our prefabs and left click on this bottom pipe prefab i'm going to exit out of 2d mode just so i can kind of see my scene a little better and within my bottom pipe prefab here i'm going to right click and add a new empty game object let's be sure to reset the transform next let's rename this to point collider now with our point collider selected let's add a new box collider component i'll left click on add component and i'll left click on box collider if you don't see it here immediately you can just search for collider again so i'll left click on this and although we don't have a mesh we can still see the outline of our collider highlighted here in green this basically means we're creating an invisible collider the player will never actually see there are a few ways to adjust its size but we'll just change the transform component let's first move it up by setting the y position to positive 10. perfect then let's change the y scale to 20 to expand it out if we left our collider like this the player would slam into an invisible wall and not know what's happening to prevent this we actually need to go into our box collider and check this little is trigger box to true next let's go back into our little tag drop down here and create one more tag underneath our obstacle tag we get this little plus sign we can left click on this and let's call it point then i'll click save and select my point collider again and then left click on my tag drop down and left click on point to assign it to our point collider game object and that's actually everything we need to set up our bottom pipe prefab and an easy way to update our top pipe prefab from here is to select the point collider and hit control or command c to copy it and then back out next i'll go into my top pipe collider then right click on the top pipe parent object and go paste as child so it's going to paste what we have copied which is that point collider here next what we want to do because you can see our collider is actually inside the pipe right now is where it has positive 10 simply go to the front and type negative 10 so it'll be coming out of the bottom of our pipe next we need to create one more method on our player collide manager script inside our player collide manager script let's go to the end of our on collision enter method and i'll space down a couple times then i'll type void on trigger enter if it pops up go ahead and hit enter if not you can still type this out and it'll do the exact same thing similar to the on collision enter method we just made unity will call this method anytime our player hits a collider marked as a trigger next to determine if we hit a point collider let's create another if statement inside our if statement let's grab our other variable name here and i'll hit control or command c to copy that and then i'll left click inside these parentheses and hit controller command v to paste it and we'll do the exact same thing we did up here i'll actually copy it to speed things up so hit controller command c control or command v but instead of obstacle let's type the tag name we just created point again remember to type out this point tag exactly as we created it in unity now inside our if statement let's copy this debug.log controller command c on your keyboard and then controller command v to paste it instead of the word game over let's just type add point just like that soon we'll connect the trigger collisions to a point system in our obstacle collisions to a game over state for now let's just save our script and head back into unity one last time back in unity you can see my top pipe prefab is still open all we have to do is back out of that to get into our main scene now if we click play one last time we should see that every time we clear a pipe the add point message pops up there goes and if we slam into a pipe the game over state will appear and our bird will be destroyed but one other thing you may have noticed is our bird player can fly infinitely high way off the map here so to fix that all we have to do is create one more empty game object i'll right click here in my hierarchy left click on create empty and i'll just type it sky limit with it selected make sure to reset your transform and let's add a box collider with everything now aligned correctly in our scene all we have to do is change the width of our box collider and move it up a bit the height i decided to use was 7.5 it just felt best to me feel free to play around with it however you want and then finally let's change the x scale to match our ground width of 50. you should see this green outline now matches the same width as our ground now if i click play again and try to fly all the way to the top you'll eventually hit an invisible wall that stops you from going off the screen perfect all that's left in this series is a way to track our player's score and trigger a game over screen if our player collides with an obstacle in the next video we'll work with unity's ui system to track all the pipes our player dodges and add points to a scoreboard if you liked this video be sure to let me know by tapping that little thumbs up button below it's totally free and helps me out a bunch as always please leave any comments or questions in the section down below thanks so much for watching and i'll see you in the next video
Info
Channel: tutmo
Views: 112
Rating: undefined out of 5
Keywords: game development, unity tutorial, unity beginner tutorial, unity beginner, how to make a game in unity, unity tutorial for beginners, unity 3d game tutorial, unity 3d game tutorial 2021, unity basics tutorial, create a game in unity step by step, learn unity for beginners, unity tutorial complete game, unity complete game tutorial, Coding in unity, unity collisions, unity collision detection, unity colliders, unity collision, unity game tutorial, unity game, game tutorial
Id: U_xvIwetwzU
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Tue Nov 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.