How to make a Video Game in Unity - COLLISION (E05)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna have a look at creating obstacles and how to make things happen when two objects collide also Before we jump into the tutorial I have a quick update for you guys I'm really glad the feedback on this series has been so positive so far I'm having a blast making it if you don't follow me on social media when you're watching this video I'm actually on my first vacation for a very very long time because of that the recent videos and the next ones are all scheduled I really tried to fit in all of the blanks while I was gone but unfortunately I had to skip a video that means that this witness stayed there will be no new video however there will of course be another how to make a video game this Sunday alright enough rambling let's get started so if we select our player we can see that we currently have a player movement script and we could definitely go ahead and add our coalition code to this script as well but to make things easier to understand let's go ahead and create a separate script let's add a component let's call it something like player collision hit new script select C sharp and hit create an ad let's double click the script to open it up in visual studio let's delete the two using tags appear and both the start and the update method so adding code to colliding objects in unity is actually really easy to do just like start an update unity has created a function with a specific name that is called whenever two objects collide let's write void just as we would do with start an update and then write on collision enter make sure to use capital letters the same way that I do and that you spell this in the right way if you do this wrong unity is not going to throw an error it's not going to complain in any way but it's just not going to be working we can then open and close some parentheses and some curly brackets this now means that whatever code replaced within these two curly brackets will be called whenever this object collides with something so if we save this and go back into unity we can add our player collision script to whatever object we want as long as the object has a rigidbody and a Collider we can be sure that this code will be called so to test this let's just go ahead and throw a debug dialogue statement that says we hit something let's save this go into unity and hit play and you can see now that right off the gate a message displays in our console saying that we hit something and you might think that's a bit weird because we don't currently have any obstacles in our scene but you need to text collision between all objects so what we actually hit here is the ground so when we hit play and our player falls down this message is display let's try and add an obstacle to our scene we can go over here in the hierarchy right click go 3d object and then cube we can then reset the transform off this object let's move it forward a bit let's put it add one on the y-axis hit F to focus on it and we can see that it's standing on our ground it's currently totally white so let's go into our project panel right click go create material and this one is going to be our obstacle mat we can make this a bit darker and we can drag it on to our obstacle we can also consider turning down the smoothness you will notice the preview down here changing the higher the smoothness the more light will bounce off the surface let's just leave that at zero we can now take this cube move it forward a bit more and give it a scale on the x-axis of about two we can rename it to something like obstacle and you will notice this right away has a box Collider so all we need to do is add a rigidbody as well we can go ahead and increase the mass a tiny bit this is a bit bigger than the cube so let's set it to something like 2 and that should be all the settings we need to configure now if we go to our console clear all the stuff we have in it and hit play we should see that we both collide with the ground and the obstacle object however we need a way of keeping track of what we head because we don't want this to trigger whenever we hit the ground to do that we go into our script and we tell unity we want some information about what object we hit we can do that by inserting a bit of code within these two parentheses let's write collision this lets unity know that we want to gather some information about the collision and then we need to give that information a name again we can choose any name you want here I'm just gonna type collision info then down here instead of simply writing we hit something let's display what we hit to do that we can replace our text with collision info we then use the dot to access different parts of this information you can see that we can get information about the contact points between the colliders stuff like the impact force sum and other handy parameters the most important one for us is that we can get info about the collider we here so that's right dot Collider and again we can type dot and we'll get all of the information about that Collider here we can access the rigidbody if our Collider the material of our Collider or just display its name so by accessing the collider within collision info and the name within the collider we should now display the name of whatever we here let's see if this works in unity let's hit play and we should see that it first shows ground and then obstacle this makes it really easy for us to have different stuff happen depending on what we hit we could write if collision info dark Collider name is equal to obstacle and remember to put this within quotation marks let's then open and close the curly brackets and now everything we write within these two curly brackets will only happen if the name of the collider that we hate is obstacle let's see if that works let's write a debug deadlock statement here saying we hit an obstacle again let's save this head back into unity and hit play and you can see nothing happens when we hit the ground but as soon as we collide with the obstacle it displays a message in our case it actually displayed - that's because we had two quick collisions with the obstacle it was hard to see with the naked eye but unity actually detected it however most of the times we'll only see one one thing that we should correct about this piece of code is that checking for an obstacles name is not the best way to go about things if you create a larger game you can quickly have hundreds if not thousands of objects in your scene and searching through all of them can be really taxing on the computer it also means that if we were to go ahead and rename the object our code would no longer work a much better way to go about things is using tags tags can be used to group objects together so in our case we could mark all of our different obstacles with an obstacle tag to do that we go up here we´d says tag we click untagged and you can see the different tags created by unity automatically we need to go ahead and add a new one hit the plus sign to create a new entry let's call this one obstacle we can then select our obstacle object and change the tag to obstacle then in our code instead of checking for the name we'll check for tag this is much better because it allows us to create a bunch of different objects and as long as they share the same tag we can configure them in any way we want let's just make sure that this is working by hitting play and indeed it is of course displaying a message in the console isn't very exciting what you should probably do is add some kind of in screen or maybe just restart the level those are great things to add but it's enough for a separate video instead let's make sure to disable our players movement because right now when we hit the object we actually just keep flying instead we can simply go in through Scribd and turn off our player movement script to do that we need to have a reference to a player movement script let's go into Visual Studio and before a function let's add a variable let's type public then the name of our script which is player movement and then we need to give the variable a name in our case let's call it just movement when we now save this and go into unity we can see that there's now an empty slot here so by simply dragging I'll play a movement script into that slot we've now created a reference from the play a collision script to the player movement script and we've called this movement now in our code we can replace our debug deadlock statement with movement dot enabled and we can set this to false again let's save hit play and hit an obstacle we can now see that our player movement script turns off so that was pretty much it for this video if you enjoyed it make sure to subscribe so you don't miss a future episode thanks for watching and I will see you in the next video thanks to have the awesome people who donated in January and a special thanks to Derrick heaps Kirk Chaisson Merrifield James Calhoun Robert Vaughn and Peter Locke and Jason Lotito if you want to become a patron yourself you can do so aperture intercom / practice thanks a lot guys
Info
Channel: Brackeys
Views: 1,394,125
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, how to make a video game, video game, #5, part 5, collision, collider, colliders, oncollisionenter, detection, physics, rigidbody, script
Id: gAB64vfbrhI
Channel Id: undefined
Length: 7min 48sec (468 seconds)
Published: Sun Feb 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.