A Better Way To Manage Collision in Unity (For Beginners)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys so in this video i want to share with you a useful tip for better managing your collision code in unity so i've got this sample scene here to illustrate the approach so this is my player and i want the player to collide with these spikes and you can see here each spike has a collider you can have a box collider a circle collide a polygon collider and if you're working in 3d you will have just the standard collider type and if you're working in 2d you'll have the 2d version you can see a 2d you can see here when i click on these different spikes they have a tag of damage the difference between this red spike and the white spike is that one is a trigger and one is not a trigger so all this means is when i play it the u1 which is not a trigger serves like a wall like a solid object and the ones which are a trigger allow the player to move through so this is all pretty standard stuff if you're working with unity so now on to the code and this is where the useful strategy is so on the player in this case we have a um box collider also which is not a trigger and we have a collision script so in this collision script we just have the start and the update we'll just delete those for the moment because we don't need it and i've just got this um damage to place and in there we'll just put a debug dot log and we'll say hit so unity has built-in collision techniques on trigger enter and on collision enter depending on if you're using a trigger collider or not and if you're using 2d it'll be on trigger into 2d so i'll just block those in quickly on collision and 2d so what you typically do here you do something like if collision dot gameobject dot compare tag and i've got a damage tag set up on those two spike objects so i'll say damage then i'll call this do damage to player function here all right and i'll do the same in here and you'll notice that these two methods have two different argument types one is a collider 2d and one is collision 2d which gets a bit confusing at times but all it means is basically you can access the compare method directly on one you see the collider you can access it but on this one you have to actually write game object and you can write them on both and it will work the same anyway just a little detail worth mentioning all right so let's quickly run this so we hit that one okay you can see in our console hit we've registered a hit and we'll go over here boom hit we're hitting those spikes so that's really good those both those collision methods are working beautifully however this and this it's a bit convoluted in in the sense that we're we're kind of doing the same thing twice you see we're checking for the tag and the tag here this can kind of be centralized and this is the tip that i've been waiting to get to and funnily enough this approach has not really been covered in any tutorials that i've seen but um it's something that i uh use often so check this out so we'll do void process collision and for the parameter we'll put a type of game object and we can just say well we can say collider that's a good way to think about it so you might know where i'm going with this at this point so rather than doing both these checks here we're going to offload and centralize that logic into here let me just change that to collider so all we have to do now is delete this stuff here and instead we're going to say process collision collision dot game object and we copy the same into there so you can immediately see the simplicity of this and and the reason this is important is because quite often these collision checks they can get quite big like i've got on trigger enters and on collision enters that if i didn't centralize it i'd be basically writing the same logic large amounts of logic text twice so let's just run that to see if it works okay so have our console open boom hit hit hit hit okay so that's working exactly as it should two logic flows centralized into one function collision managed in one place very cleanly so what you're actually looking at here is a new unity asset i'm working on now the purpose of this asset is going to be to be a useful starting point for people trying to make uh platformers so it has a responsive mario style jump and this is actually the kind of same system i'm using in my own project blood and mead this it's got a very precise platforming collision and it has you know running jumps like you would find in mario so your big jumps and importantly it has mobile controls built in we hopefully this project will be complete in the next week or so so depending on when you're watching this video check the description below and that link might be available so that's it guys i hope you found this video useful if you have make sure you give it a big thumbs up down below subscribe to the channel if you haven't already and if you like come by the discord we have a nice group of developers there who are engaging in different interesting creative ideas around art music game design coding and all things like that so all right guys all the best see you in the next video [Music]
Info
Channel: Lost Relic Games
Views: 25,814
Rating: undefined out of 5
Keywords: collision in unity, collision, unity2d, unity3d, tutorial, how to, unity, ontriggerenter unity, oncollisionenter unity, learn unity game dev, lost relic games, john stejskal, beginner unity tutorial, game dev, start game development unity, make player shoot, player controller, tips, for beginners, oncollisionenter2d, Ontriggerenter2d, start unity gamedev, game dev for beginners, c sharp, unity collider, collision detection, game development, unity trigger collider
Id: TRvnN4bfAxM
Channel Id: undefined
Length: 6min 16sec (376 seconds)
Published: Fri Dec 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.