Break Apart and Explode 2D Game Objects in Unity! | Game Dev Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Cool! But I was hoping i could do it procedurally without making the sprites :) 1 hour tutorial seems a little lenghty so didnt watch it all. Could it be done in 10-15 minutes?:)

👍︎︎ 5 👤︎︎ u/nicmarxp 📅︎︎ Jul 25 2019 🗫︎ replies
Captions
alright guys welcome back to another video this is john from lost relic games so in this video we're gonna look at how to destroy crates and barrels using rigid body physics if you want to follow along with my sprite sheet i'll link it down below in the description and though this video is a little bit of alongside i did so intentionally so i can get into the nitty-gritty of the process and reveal some insightful knowledge along the way i hope you guys enjoy that if you haven't already make sure you subscribe to the channel as i've heaps of the game to have coming your way alright so let's get going [Music] so in the last few videos we've been printed together this little scene here which has a play controller with a camera that follows a player and in the last video we did this melee combat script which lets the player throw random attacks and punch the crap out of its barrel so in this video I want to look at destroying of this barrel in some kind of a visually interesting way and we'll be doing a bit of Photoshop work breaking this barrel up and then I'm using rigid body physics to disperse the key components for the air so let's get started with that so if you don't currently have a scene to work with you're welcome to go back to those past few tutorials and catch up otherwise all you need is some kind of a player that can destroy something whether they have a punching attack or a gun that shoots it doesn't matter if you've got a game that needs something destroyed this tutorial will help so let's have a look what we've got here we've got the barrel and a barrel sprite sheet here and we've got a crate here tube which we'll be using just to create a bit of farm variety in the scene and both these two components will be using the same approach and script so let's open this guy up in Photoshop and you can see in here I've got two layers one with the crate one with the barrel so what you'll want to do first of all is kind of give yourself a bit of room to work in my case my stage size he is 256 by 256 so you want to try to keep it in power up to 512 by 512 1 or 2 4 by 1 or 2 4 or any combination of those 256 by 512 etc so let's get this show on the road so what we'll be doing here is first of all I'll duplicate this layer with ctrl J and then using this polygonal lasso tool I'm gonna come in here zoom in a bit and come up with an interesting way to dissect this barrel and one thing you might want to do is kind of follow the lines of the shape so that when it breaks up it kind of visually makes sense or not it I mean it'll happen so quickly that it doesn't really matter too much but it can be nice when you see it break up along the lines so we've got one section so we'll do a control X for cut and then control V to repay stit so now we've got a new layer over this little section here so we'll just move that a bit to the side here so this is actually the same approach I'm using in my own game called blood and Mead you can see we've got sprite sheets here of broken up assets very much like we're doing in this current tutorial and now let me give you an idea of what this stuff looks like in action [Music] call destructable czar speaking of which blood and meat is currently available to wishlist on Steam if you guys jumped over there and gave it a wishlist I'd be so grateful it'll help the steam algorithm to notice the game I'll drop the link in a description below so thank you for your kindness and let me return the favor with this tutorial for you and we'll continue on with this lower section so back to the less suitable here the hotkeys l for that and we'll holding shift we can snap to a hard line and maybe we'll break this down the middle hold on let me just deselect that so hopefully you guys know a little bit of Photoshop this isn't exactly a Photoshop tutorial that's kind of um assumed knowledge you can say if I was to do a Photoshop class it'll be very extensive but that's something if you guys want to know about that there is something that we can look at in the future okay so once again home control X to cut ctrl V to paste onto a new layer and now we have these three components so we'll just move these away from each other a little bit so that when we take it into unity we can slice these things up without any issue okay so let's quickly do the crate here so ctrl J to create a new layer and then once again click the lasso tool and this can make for an interesting shape you might cut it along the lines here so I mean you can break these things up into a lot of pieces and the more pieces you have more interesting it'll visually be depending on the game you want in this case I might try to keep it to three pieces I'll try to make it look like it's kind of breaking unnaturally or rather along the organic lines of the wood of the crater rather ctrl-x paste it onto a new layer grab this lower bit here I might just move it over here so we away from that other barrel maybe something I don't really like that too much maybe something like that you guys think how's that look you guys don't exactly know what I'll be doing with it just yet so that question is sort of irrelevant all right so we have our pieces here I think this is a pretty reasonable start and we'll go to file export basically just whatever method you're using to export and find our initial sprite sheet and just overwrite that replace boom okay back to unity so now we go click back on that sprite sheet and you can see here it's updated at the bottom it's got the new pieces so first of all if you are working in pixel art make sure this point filter is on if you're working with vector art if you feel free to have bilinear or trilinear having point mode will make the pixels very crisp and sharp so anyway continuing on will hit sprite editor and you can see here we've got the base barrel and crate that is the while it currently in the scene that's being shaken so here we have these pieces now all right so we want to slice them up so we're going to be sliced tool and automatic is fine rather than delete existing I'll say safe that'll keep our existing - what do you call it slices here without deleting them hit slice and now we have these sections here when you slice things up in unity you always just want to go quickly back in and make sure that it's being sliced up okay you can see here that there's a bit of a 1 pixel and here you can see it hasn't sliced quite perfectly check this out over here that looks okay that one looks okay what's bump that up and we shall hit apply close that often and get back to our scene so this um sprite sheet for both the player character and these barrels will be available in the links in the description of this video so if you don't have something to destroy feel free to jump down there and grab those open them up in Photoshop and feel free to follow along exactly how I did it it's a good starting point right so here we're gonna do it now we're gonna blow this thing up so how this works is we're going to remove this guy from the scene this existing barrel and we're going to replace it with another game object which is made up of these sections and then use a script on each of those components to push them away that's how I like to do it other people may do it differently there might be I imagine in unity if you're a unity user you'll know that there are a ton of ways to do things in unity and each has their own style this is one I came up with myself that I quite like and it's the way I do when it works for me so one trick what we'll do here is select the barrel in the scene and right click and say create empty and this will create an empty object within the barrel what we're going to do then is to then drag it back out of the barrel so it's a child of the actual whole stage why I did that is because if you now if I now click this in your object and look at the pivot point it's aligned perfectly with the barrel so doing that right clicking and Cree a child of that object and then moving child out of the object allows us to map the same X&Y position without them fiddling with the coordinates in the top here and trying to align things perfectly so this new game object I'll just call it a barrel destroy so anytime I do this destruction stuff I usually like to end the word with destroy just so I know that it's a barrel that can be destroyed that destroys first a crate we need the barrel pieces okay so what we'll do now is shift and click and select all three of these pieces and drag it into this barrel destroy clip no no okay so my mistake when you group select things in unity tries to make an animation so we have to do it one at a time guys so click one drag it into the barrel destroy grab the next one drag it into the barrel destroy and grab the final one and drag it in also and they've just moved themselves over here somewhere so what will now do grab these sub components and position them back on to the barrel matching the shape that way when this thing blows up rather than just a mishmash of things flying everywhere it'll actually look like an egg breaking and that's what makes this system very very this Maxis approach very very cool because it'll break apart and actually look like it's some you know really breaking and not just a bunch of particles and crap flying everywhere so this doesn't have to be perfect as part it's kind of um you know there's eyeball it and that looks okay so now we want to make a prefab out of this object because we're gonna be um loading it in at runtime when that original barrel gets destroyed so to do that grab this barrel destroy and drag it into your prefabs folder okay so now we can actually remove this barrel from the scene and so we have an existing script on this barrel I'm thinking of removing this script and doing it again we touched on in the last tutorial but there might be a few people coming here for the first time wanting to know how to break this thing up even kind of make the initial line take damage so I might do that guys so in my assets folder I've got this barrel scripts I'm going to delete it okay so what we'll want to do first of all we want this barrel to take damage from the plate so to do that we've got to do one thing first right and this the same if you have a bullet or a gun or a weapon that you want to destroy the barrel with and in this case our player has a attack hitbox here which gets turned on when the player attacks I cover that in detail in the last tutorial if you want to learn how to do that but essentially what happens here when the hitbox gets turned on it makes contact with the barrel here so to do that we select the hitbox of our player you can see it here if I edit the Box Collider you can see it right there and you'll notice that it has no tag so the first thing we'll want to do is give this a tag and you may probably need to add a new tag to your scene if you haven't got one already if you have an existing game where the player can shoot or punch then you probably already done this so click add new tag then over here we'll click the little plus arrow and here we'll say I'm gonna call it a weapon I'll do uppercase weapon is kind of generic you know it can be can be a weapon it can be fists it can be something else and now that we've created that tag we need to assign it to this hitbox and once again if you have a bullet or a gun that shoots find your bullet and call it weapon or if you've already got a tag you can use your own or foot one for the next step so now we want to go back to our barrel click Add component new script and I want to call this barrel scripts but we're going to be using a crate too so we need a word that works for both how about we call it Crate and Barrel script you might want to find something a bit more eloquent than that let's say but oh no I'm going to add that to the script sorry guys so add new script and here the name will be Crate and Barrel scripts create an ad and that's added it now to this barrel so make sure first of all that you have a box Collider assigned to this your object so you can see here I've got a shape and to do that you just go to add component box Collider 2d and I should automatically map out the shape of your sprite and you may want to also add a rigidbody to it if you're working within a rigid body space I sorry a physics space this game is using the Unity physics so it needs a rigidbody for collisions because my player has a rigidbody it's a bit of a thing in unity if one thing has a rigidbody the other may one also have a rigidbody if they interact with each other doesn't always have to be the case but I'm going to do it at this time so click the barrel and double click on the script down here let's open it up in Visual Studio so we'll start by defining a few properties at the top here we'll have a integer int quality health and this would be the health of the barrel and this we how many hits we wanted to take before it explodes and we'll say maybe maybe you know what let's start with one just to make this really badass so the moment he gives it a hit things come in just go everywhere so health int because y know we can serialize that and this makes it available in the property inspector so we can edit it there and fine-tune it will also want to now create a reference to the game object where the destructible barrel game object we want to load in and to do that we want to create a property of the type object you can start typing OB you should get a order complete from visual studio so note there are two objects you have this small lowercase object and the up case you want the uppercase one and if you hover over it you'll see it derives from the unity engine that's the one you want some people get this confused because there's more than one type of object so here we'll say destroy destructable ref so it's a reference to the destructible object and it will also serialize this field and we need to now check for impact with the player will do on trigger enter 2d because the hitbox that we are going to be interacting with is a trigger that's that one that was on the fist in your game it might be a not be a trick it might be a half box Collider on a hard object let's say so when you walk into it it's not invisible if that's the case then you may need to do on collision into 2d so here we'll say if if collision dot compare tag and here we're going to pass in our tag a weapon that we assign to our hitbox so if the collision object colliding with the barrel has the tag of weapon then health - - so our health is decreasing and we'll say if health is less than or equal to zero then [Music] explode this game object again these function names I'm using are kind of verbose and long specifically for the purpose of explanation in my own games I'll probably write something a bit more shorter and concise but this just helps people to understand what this function does so we'll need to make a function based on its name this a couple ways you can do this you can hover over this thing and you'll see this drop down pop up and you can see click generate method and that all boom look at that it's given us a function so I'll just delete that show you another way if you hover over it and hold down control and tap the period button or full-stop you'll get the same thing here alternatively you can just write it yourself and then you have to remember exactly how you wrote it and I'm not gonna do that so when Auto generate this function and this is where the fun stuff starts because we are going to be loading Oh what does it say object is ambiguous reference between well it shouldn't be if you get this air up here which can sometimes be the case what you can do you can explicitly tell it what kind of a game object and there will be unity engine dr. game object so saying make sure you use the game object from the type unity game object sorry guys then you realize that was an error coming up there so that would just um avoid anything having a problem later okay so here we want to load that destructible game object into the scene so we'll say game object destructible equals game object in brackets and this is what's called a cast instantiate and here will pass in that destructible ref that we defined here at the top okay so when when this barrel gets the health hit zero we fire off this explode this game object and here we load in the destructible game object and we will say destructible the transform dot position equals transform dot position and what this means is map the the new newly loaded destructible objects to the x and y position of the previously destroyed barrel Alesana that doesn't explain it for you then I'm not sure it will all right and what we can do here now we can actually get rid of the previous barrel because we won't need it anymore because we do in the old switcharoo you see we've got this barrel that one gets destroyed and we swap it in with this one that one's gonna go sold to destroy game objects and we want to make sure that this happens at the very end because if it happens earlier than this other code won't run look okay so we jump back to unity now and we'll find that will find the barrel prefab here or click on it however you want to do it and you see the destructible ref is none it's empty so what we're going to do here we want to find the barrel destroy which is a destructible object as to remind you guys this is the object that we have all the sections for so we want to click out the one that's not broken look at the script we created and drag in the barrel destroy over here and this will get assigned to the object in the script so let's now actually see what will happen when we run it no errors good Oh Bom Bom Bom Bom and AH you can see he did actually load in that object so it worked and you can see here in the scene how the barrel from before it's gone it's been destroyed and it's loaded in this clone object which has those three sections so that's cool but it's not going everywhere right so we halfway there we have this barrel that's getting loaded in which is um I'm cool in its own way but now we have to make that thing destroy so how do we do that so grab this bowel destroy back into the scene right and expand the hierarchy so we can see the different sections one two three we want to do a couple of things here first of all we want to create some colliders so much guys might just pull these apart a little bit probably should have done this first you know it's always hard to know that order of what to do but all roads lead to run in this case so we'll click on this top one and we'll give it a box Collider 2d and you might always click on edit Collider over here you can see the collider bounds where'd it go you want to just maybe change that so these collider bounds is when it's going to hit the floor and bounce around it's going to use this boundary so it's okay to kind of pull it in a bit in some areas and you'll know what I mean later on for now you know what for now maybe I'll just give it a bunch of our box colliders and later on if we have an issue we can come back and sort it out what do you guys think so at a box cleaner - that one - so they all have box gliders now we also want to add a rigid bodies to all of them as well so rather than going one by one to all of them what we can do once again is um select one two three holding shift and go add components and we'll say rigidbody 2d and if I hit play I'll show you you guys what this will do it it should just kind of fall apart we can't see it yeah look at that this is kind of um falling apart if I grab all of them just move it over here and unpause the scene you see they fall apart it's sliding around a bit - it's probably not what we want so I might just check this out I'll select all three of these and looking at the rigidbody we'll create some maybe some linear drag so bring that up down at 50 just want to see what happens if these things keep sliding around or not I'm not quite okay let's change the master to of that one no it's linear drag is not happening what if we got for that one must end linear drag turn thank drag no I will try that once again as I always say with unity it's a matter of trial and error you cannot be expected to remember what every single tweak here is gonna do and how it's gonna affect the game you have to just play with it and find something that feels right for the car into context okay so that was kind of okay but I feel like it's not falling quick enough boom that's better that looks okay but you say that poses another problem that you might arm realized moment this thing's explodes it's gonna be running into us and we are gonna have to deal with that but first let's destroy this barrel so we're gonna map these settings we created for this one so it was like it was a mass of ten and a linear drag or five and what you can do here because I only applied to this one here you can click over here on this COG and say copy component then select the other two one two and go over here to this they're rigid body components back to the COG and say paste component of values and this will see that it updated the values for those two things I didn't have to manually go in and type them in so let's get back to it so it's got to move these back into place because I just broke them apart so I could work on the rigid bodies a bit I hope this is a making sense to you guys it's a bit of a finicky process a little bit complicated but hopefully by the end you'll have a good reference point that you can go back to so what we might just do here we'll leave this in a scene temporarily and fine-tune this so that we don't have to keep hitting this barrel and doing it that way what I might just do a bring our player over here so the next step of this is to break these sections up so we need to create a script so select any one of these components let's grab the top one again we'll go down here and we'll create a new script and we'll call this one this is what I like to call it in my games destructible rigid body and once you create this script you can continue using it in other games and I use this thing everywhere I drop it all on different objects destructible wagons destructible enemies destructible barrels and I keep reusing it everywhere it's very useful script very useful simple utility script so we'll double click and open that script up so we're going to start once again by defining a few properties here at the top first one we'll make a vector - I'll call spawn a force direction and once again we'll see realize this thing exposing it in the property inspector and the other one we're gonna do is caught is a float maybe we're making an int that gonna need something seems to be a float in case you don't know a float is a number with decimal places and an integer is a whole number so 1 2 3 4 5 6 7 8 9 10 is a integer and 5.5 3.25 etc is a float so call this one talk you might be familiar with this word Tok Tok is on the spin factor of something so the serialized is filled as well create another property rigidbody2d and for short I like to write our B to D and in the start function we'll assign the rigidbody2d to that so our B to D equals get components and a rigidbody 2d opening closing bracket and close it with a semicolon so now have a reference to the rigidbody so the moment this so the moment we lo load the destructible rigidbody into the scene the one that comprised of three objects these scripts will all fire instantly in a start function we don't have to do anything else that will run automatically so here we can immediately say rigidbody 2d dot add force here we'll pass in that force Direction property we created up here so they might give you a hint on what's going on here when this thing hits the start function automatically we're going to tell that rigidbody to use this force which we're going to assign in a moment and we'll also give it a talk so say our B to D dot to talk and it takes a float which is interesting that I signed it as a integer so we can exchange that unity like sense of float so we'll make it a float just for consistency no big deal and I'll pass in the talk who created and that looks good we can actually delete this update function because they're not going to be using it in this case so let's go back to unity now so we've got our force direction and our talk and we've got three different objects and the trick here is to manually set these so that they make sense visually so we want this guy here to fly out that way we don't want to fly it that way it wouldn't make any sense because when an explosion happens it has to kind of follow the pattern of the explosion and the mean velocity from a center point and this way we want to kind of fly up and we might kind of roll it a bit to the right and the last one will kind of shoot out that way so let's do that now so we'll start with the right one this time and lo and behold we don't have that script attached because we only attached it to the top one so let's start with the top one so here in the fourth direction if I change the force direction to 100 and the torque let's do something silly no talk to 500 and run this I'm predicting the top will pop off like like a coke can lid under a lot of fears oh it went the other way and I was not too impressed with that spin so let's go higher let's go 300 force direction and minus 500 talk oh no what - 5000 gonna do it do it right yeah oh my god it's shot out of the screen so hard that will you get to see it and because it's a physics object the the rigidbody spent so fast that is finger spun into oblivion so we're going to be a little bit conservative here I was trying to admit to make a cool spin but I think I'm just going to be too crazy with the numbers so we've got eight hundred and three hundred let's see what happens there we go let's run that again it's kind of cool huh that's not too I kind of like that I think that we kind of punch they keep that I think just pops off like that so my just changed the torque to a little bit less 500 change the force Direction up to about 200 and now let's what we can do we can either copy the script over to the other ones or we can click on the other ones and click Add component and what did we call it we call it a destructible rigidbody so I'll click on that script and we'll do the right one now just such tip guys you might want to rename these I'm still using the same slice names from the sprite sheet as a result I've got to keep clicking those if you rename those like um top left and right might make life a little bit easier okay so the right one we want to kind of shoot off that way depending on which way the camera is facing so say 100 and we might just give it just a little bit of talk not too much so I'll say maybe it is 20 and we can give it him give it a little bit of vertical momentum just a bit we'll say 10 so I'll do our left one here force direction so importantly here we're gonna use a negative number for the force Direction left so we'll say minus 100 and talk I mean we don't even need to talk for that one we'll say zero just to give it a bit of variation so let's run that those did not run why did those two not run is there something to do with this top one might just turn this top one off and see what happens so they separated not very convincingly so I think we need a lot more physics and this is partly to do with our rigidbody settings here because we've got um see if I change the mass to five five so they're breaking up a little bit more I don't know let's go you know okay yeah I'm not sure what was the issue with the gravity scale before cuz I remember I tried um I tried having a lower mass and he was having some issues so I might try that with the top one as well let's not turn that back on okay so what's happening here you hold on when the top line is been flying well here he comes here he comes there it goes boom okay if there's bouncing fair enough it's bouncing because of the spin factor it's got so much spin to it so we'll change that to two and I want to change that mess back up to two for everything getting there guys we're getting there so much has changed force direction for that one to 200 and it's what - 200 but what's happening is that because these objects are so close together they're actually their box clients are touching a little bit and it's causing some friction between the two and that's slowing them down we're going to saw that in a moment but one quick way I can sort that out is by just kind of separating them so that their box colliders are not touching and that really shouldn't make too much of a difference visually thank you see that time it worked much better so we just want to run that again get a visual look so you can see the left and right ones they're kind of breaking in a similar way and it looks um doesn't look very correct in terms of physics so that one's got a torque of 20 and then one's got a torque of 20 maybe because of the talk okay I'll give this one a the left one a talk of -20 see what happens okay that looks all right that top one's spinning like a helicopter though it's cool but I think I need to drop the talk down a little bit okay that is reasonable so that would work so what we want to do now what apply the changes we've made so what we'll do click on the prefab here if you don't know about prefabs I've made I made a very good video which is a comprehensive guide of prefabs in unity that explains how to do everything is with everything with prefabs explained including what all this stuff means but essentially we've made changes to the kind of template for this and we want to hit apply all so that gets saved to the one in our prefabs folder here battle destroy so am I just delete that temporary from our scene see what happens now when we punch and destroy this one here what I think's gonna happen is because the player is interacting with those destructible pieces but they're gonna hit the player and they're going to bounce off the player and there's going to some issues there yeah so that sub component didn't get to shoot out too much so how do we get how do we resolve this we need to put them on different layers guys that's the only way to do this so if you click on your player there's no layer assigned it's as a default in any game what you want to do you want to typically put your player on a layer called player and your call it action compaction components barrels and enemies onto another layer you may have multiple layers and what this is what this does it gives you the ability to to control what collides with what so we can say yes we want the player to collide with the ground we want the barrels to combine with the grant collide with the ground but we don't want the player to walk into these broken pieces so let's do that quickly now so first of all go over here to the default drop down and but unity creates a few default ones and I've actually already created one which I'll show you what that was okay so we'll create a layer called player actually you know what we don't even need that so create a layer called ground only so this is a useful trick I use in my own games for things that I want to debris in clutter that I want to fall on the ground but not interact with enemies and things like that because it's gonna muck up the game flow so call your layer ground only this is a layer I made previously which happens to still be here okay so go into yeah go into your layer and make it ground only and then find that barrel destroy prefab and you can double click it into prefabs folder here and go straight to it here and what you want to do is click on all of these separate sub components and go to the layer drop-down and select that ground only layout and as the name suggests we're gonna make these components only interact with the ground so they'll bounce off the ground I'll land on the ground but they won't touch the player the parent object can still be on default I believe we'll see what happens okay so when are we done with that we can back out and one more step go to your edit settings go to project settings and there will be a physics 2d drop down here and when you click it you get down here this collision matrix and what you want to do here by default it will look like this and you you can see it lists all the different layers we have including this ground only layer so those are sub clips are assigned to that layer so we want to look at this matrix and say we only want to let the ground only layer only collided with the ground so when you hover over each of these boxes it tells you what that tick box represents so over here you Y if I have that ticked the UI can collide with the this ground object so we don't want that even though we haven't got any UI great for if I select the ground only layer it means that those components can collide with the other components so that debris will bounce off itself which is kind of cool in some cases but in this case we want to just exclusively use the ground colliding with those ones so ignore the other stuff for now that stuff is not important and do not try to change any of this stuff in here because you can make your unity physics engine to go pretty haywire so let's now check that again one boom okay so you could look we can now walk through these components and they're not interfering with the player so I might just bring this barrel a bit over here and let's make a few of them that's copying pasted a few times like that bang oh oh whoa that's cool that's cool that's cool but what do you notice they're all being destroyed in the same way and it looks a bit to put it simply because it's some visually catching the eye and it looks very repeating so we want to kind of make it a bit more unpredictable and random so we'll do that now and we'll go back into this which is a we want to find that script was called destructible rigidbody so you can do a search up here in the search box find a script double-click it to reopen it and we'll want to create some randomness to it it's trying to think how best to do this float round talk equals [Music] Unity engine dot random-dot range and we can even try to think if we should just pass in a completely random variable here because we do have because if we make this completely random then it's just it's gonna do its own thing right it's not going to follow that kind of eggshell explosion effect that I'm trying to keep here so talk is okay talk we can have as a random so we'll say - - 100 - 100 so the torque will fall anywhere between minus 100 to hundreds of so that might be a left flip or a right flip and we'll pass in this random talk over here but for the force direction it's gonna be a little bit different perhaps so I'll say oops sorry caps lock on so I'll say around force x [Music] equals so what will do will we can leverage the existing values we've put in here just so we're not we do have to hard code all the minimum and Max price or say random force x equals unity engine dot random range and we'll say force Direction dot X minus 50 second parameter force Direction dot X plus 50 so we're using the values we type in there it's sort of like an average point average mark so we kind of want to still have that general direction and force by we then will offset it by a bit of randomness and this may or may not be this may or may not be too much but we'll see so copy and paste this line and we'll make this one the random force Y and similarly we'll do random force direction because we're always going up what I might do I might just leave that as random force Direction not y2 random force Direction plus 50 I think that'll work and we could create a new vector to here and pass it in or we can apply them straight to that so so random force and Russian dot x equals random force X force Direction dot y equals random force Y so let's take a look at that boom okay boom they would they will spawn boom I'm getting away to spin out of too much spin out of those though I just want to change that up a bit random talk yeah okay so I'm going from that to that and it's how about we go no more than minus 50 no more than 15 either way because when it spins too much it um doesn't look that great boom boom fly keep this one gang so that's pretty cool I'm gonna do that again I quite enjoyed that and I might put a few more of these barrels around [Music] yeah whatevs that's pretty cool I like that the weight of the barrel is a bit off overall I might just change that quickly so you just enter that prefab by double-clicking on any other barrels that's what the mass of this man let's make it Bobby there's a linear drag thing that we had issue with last time oops sorry let me go into the prefab and you drag the change up to five okay boom boom all right very cool so let's make this even better it's a bit of fun a bit of bonus content I want to have some particles explode from this thing because it's nice type of breaking apart but it doesn't look like a much of an explosion so I've taken the liberty of creating this particle effect here see that i'm let's play it again it's a bit dramatic though i need to just rain that back so I'll make this some prefab available for you guys if you check the links below so I'm just going to change the life start life time to minus one and point three that's a bit better finally going less than that point one point two okay as further so as mentioned check the links below and I will make this available for you guys to use so what we're going to do with this so we've got that in the prefabs folder over here so similarly with how we loaded the other one in on top of the barrel we're going to do the same with the explosion so go back to our cranium barrel script over here and we'll add it to this destructible ref and we'll say particle X blow Z on ref once again you can just call that particle ref or explosion ref but I'm just writing it this way to differentiate it easily for you guys and once again and in just in the same way we loading in this one here we'll make another line and we'll say particles and we instantiate particle explosion particle explosion ref and once again here we map the position to the center of the explosion and what do we have to do now do you remember we have to drag this into the barrel prefab so if we find this barrel which has the way the script so we've got this script here right and we've got the destructible riff dragged in and now we have to drag in the particle explosion so once again guys you want to drag that in here someone actually just call this particle explosion let's get the naming a bit more consistent back to that script and we'll drag in the particle explosion here the other way you can do that is by clicking this little knob down here and then you can find it that way the other thing is I just need to save off this prefab because I've noticed that these things have edits but they're not saved okay hey-oh so this is looking pretty good pretty crazy there's a lot of clutter everywhere so in your own game if you're doing something like this you probably want to destroy some of those pieces after we can do that now actually so if we go back to our we just jump back to visual studio if you go to the destroyer rigidbody and we can create a function called destroy self and in there we just say destroy game object so we can just call this after a little bit so say an invoke and pass in the name destroy self and we'll do this after you can do it even use a random period so they kind of randomly disappear Unity engine dot random dot range oops and we'll say destroy this thing between 1 and 4 seconds one thing to note with this random system in unity if you pass in integers it will use integers to make this calculation so this will return Hardy to integers between 1 and 4 however if we make these a float 2.5 and 4 f putting an F after it that'll indicate that we want to roll a random float so this will roll any kind of number any decimal place between that which is a bit better it would be a bit more random so let's see what happens now and to answer that you may be you may want to fade it out or something using a tweening engine or opening up boom-boom okay look see they're all so that's a little game itself guys maybe this little thing and as always if you want to get access to this complete project with all the assets and all the code ready to go you can jump over to my patreon and help support me there and I give away not just the code to this tutorial about every single tutorial that every single project I have currently in the past and into the future so that's a a nice thing nice way you can support me and also get access to everything without lifting a finger which kind of defies the purpose of learning but you know each to their own so it's almost a little game if we put a time limit here it's like destroy all the barrels in you know less than a minute that's already a little game that can be the next flappy birds guy's got some issues there so what you can also do these barrels I can make them on the ground layer that way that play can actually walk on these things oh yeah I can jump on top now awesome so what else can we do how can we make this even better what is this crap error what can we do so one thing we can do we can shake this barrel and use the health give it a health rating rather than just having it set to one so what we can do is change this help to three yeah okay we'll make another property called sort of your boolean is shaking equals false and we might just move this down to here it's usually good practice that put the public stuff or serialized stuff at the top we'll make another property float say shake amount let's say point 2f so in the update function will say if is shaking then transform dot position equals alright hold that thought guys one more property we need I need a vector to call starts pause I mean start function I'll say start pause equals transform dot position so the current position that's the start position and is shaking transform position equals start position plus unity engine dot random dot inside unit circle x shake them out see if you've watched the last tutorial I went into this once already but all this does is it tells the unity to find a random position inside a circle and that's some nice way to do it so we decrement in health so if we make if we collide with the weapon and first thing we'll do if - okay so if the health is less than zero we explode and destroy this thing otherwise if it's not ready to die yet then is shaking equals true and immediately after that we'll say invoke and we'll say passing a function that we don't you to have called um reset shake and we'll call this after ever like 0.2 it's a very short amount of time and we'll make another function down here called reset shake and we'll say is shaking equals false so you can run a code written here you can kind of delay it some other way but invoke is this easy for the purpose of this tutorial so if is shaking equals true then do that and after a point to our second stop the shaking oh and most importantly when the shaking finishes return the transform parkin trend its form dot position equals start parts we want to return the position back to the starting position that waits otherwise it's going to be shaking and move across the screen we want to kind of shake it and return it back to where it was so back to unity in this should be interesting oh that is not working of course it's not healthy cause three well it's Topsy realizing that I've got a feeling that these barrels are all mapped yeah they've all got a I've mapped a health of one to all of them that's the thing when you see realize something you will override whoever's in the code here so I might just change you that take the serialization off and that'll make it so I can't override the health from the inspector so now it will have a health of three notice that there's some errors here though it's not affecting the game it's still working but why is it pointing to that unassigned reference the very old particle exposure has not been assigned you need to assign other I of course have assigned it of Isaac wouldn't be blowing up like that it's assigned right what the hell so I don't know how this happened well I've got a few of these scripts running at the same time it's gonna go into here and troubleshoot this remove not sure what happened there sometimes you get these little glitches in unities when applying prefabs I don't know and that still has that move applied all the others okay that should be alright now errors are gone boom Wow oh it's a big shake ball so let's just scale back that shake a little bit and we're very close guys it's gonna be really cool I'm really happy with how this has gone it's been a bit of a long one but as you guys can understand it takes a bit of a bit of effort in nuance so where's the shaker mount we've set here so shake America's point to maybe there needs to be point o2 so yeah back to unity run the scene bang look at that you see we get that little nice little shake and one more issue it's resetting the position so rather than the start position being mapped is there I've got to move that down over here guys so when the player punches the barrel that's where we want to set the start position let's run that yeah that's feeling pretty nice just imagine we've some cool sound effects double kick in the air oh wow this is actually quite fun guys I must say here this is a really fun minigame should stick it on itch or steam making million bucks just kidding to finish this up I want to bring in that crater we created in the first step just to create a bit of diversity here in the level so that very very efficient quick way to do this so pay attention to this guy this is a very cool way so we have a lot of these barrels but we don't yet have the crate the crate is very similar it's going to use three sections it has a box fly of the rigidbody so rather than going making all that from scratch what we can do we can grab one of these barrels here and click unpack prefab completely okay and now what we can do is you can notice here that the blue boxes is gone so it's no longer prefab so what we can do we can call this crate and over here in the sprite renderer we changed the barrel to a crate and we just tweaked the box clutter by clicking that icon there just like that and that like that and you can see we already have the rigidbody set up and what I'll do now I'll drag this crate into the prefabs folder making it a prefab and the funny thing that will happen though now is because we are running the same script it will it will break up into the barrel pieces so there might be quite interesting just to see ok cool huh so what will quickly do similarly to how we just replicated that they all destroy so drag the bowel destroying to the scene right click unpack prefab completely rename this to create destroy and in the top right bottom left just got to change the sprite for these so you can see why it would have been better if I had renamed these in the sprite sheet because now I'm have to flick through thankfully we only dealing with a few pieces so it's not too hard so that looks about right and the right piece okay it's been kind of bring these closer together box clutter on that one is a bit funkier so I just quickly edit that box cleaner a bit alright so create destroy drag that into the into the previous folder we can delete that from our scene now that one and on this crater we created on the script we change the battle destroy over here to create destroy apply the changes now we can duplicate this a few different things in the level so this really should just visually make it a lot more interesting to look at so let's run that and see what we've created I'm pretty excited for the skies this is so much fun I can't tell you how much fun this is there's something about punching and destroying things double kick double kick in the air Oh No kick it let's get up there bang bang oh this is so much fun this is all you can just imagine this with sound effects how absolutely awesome it would be is the next hit game guys so you made it to the end good for you it was a bit of a long one but I hope you found it enjoyable if you have make sure you do give it a like and subscribe to the channel if you want to get you know more game day videos like this thanks to all my current patreon subscribers all you guys are beginning the full project source code and files for this tutorial as well as all the previous project files if anyone wants to get in on that I'll link my patreon down below I appreciate all the support I can get for this small end growing channel thank you very much guys see you in the next video and until then keep it in me [Music] you
Info
Channel: Lost Relic Games
Views: 16,952
Rating: undefined out of 5
Keywords: unity 2d, break apart game objects, explode game objects, break apart, explode, rigid body, physics, how to, how to explode objects in unity, how to destroy things in unity, destroy things in unity, indie game developer tips, 2d player controller, unity project setup, 2d platformer tutorial in unity, c# (programming), game development for beginners, learn unity fast, unity courses for free, game dev classroom, unity 3d, series, particle, melee, animation, setup, crate, loot crate
Id: oCDsDb7V59o
Channel Id: undefined
Length: 76min 39sec (4599 seconds)
Published: Wed Jul 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.