Shatter and Explode your Objects - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so hello and welcome back to another unity tutorial and this one I'm gonna be showing you how to make this effect that I've used in my game and you can apply it to many other things the effect quickly is part of the spell where I have in my aim the game that I'm making I have a sparkle rock curl where you click on the ground somewhere and it makes a rock go up into the air it's up there now and then it finds the closest enemy and it homes in on them and then the rock flies towards them and soon as it hits them it smashes into pieces like that and quite a few people asked how to have this like Rock shattering effects I have enough see this is what the videos are going to be showing you like how to make this happen obviously like I just love how this looks it's hard to get the exact frame I don't know if I can you go frame by frame in media play or whatever this is it's video thing obviously I'm putting in real time that looks really good and I'm showing you how to do it for any object you have as a model in well bunda I'm gonna be showing it in blender this will be possible in other softwares but I know blender only really so all you need for this tutorial is a model to shatter so I have this this nice Rock here and blender specifically well not specifically but preferably be a lot better if you have a blender and what we're gonna do is we're gonna make it I'm not going to create the whole ability but I can make a cube and use this as a like flaw and we'll just do the usual like spreading it out like this and because it's very bright we'll give it a material so let's do this and then we will go to the camera make it solid color and we'll also move the camera but okay so now first thing we'll do is we'll just have the rock falling so here's our rock that I've made him blend or whatever I just have to have your model ready we want to we don't want import animation or anything the materials don't matter for me and then now once we've done that we're gonna put it in the scene now let's just drag it up so preferably if I pull this it like zero zero zero zero zero what we'll do is well put it a bit closer and up I'm gonna move this in that direction so now if I push this up a bit more what we're going to do is we're going to take off the animate so we don't on that will leave the renderer stuff on we're just going to simply add a mesh Collider so that's for the rock and then we'll put we won't put trigger on we want compacts and then we'll add a rigidbody now we don't really care about the actual physics as long as it Abbie's gravity I mean unless it falls too slowly I won't bother tweaking the mass so let's just press play so this is what would happen normally the rock falls and it hits an inter self rolls over or whatever now in my opinion for a rock that that mass is very low it just kind of slowly falls down so plunk how about can we actually just go to the physics settings and just like increase it by a double there we go that's kind of more realistic so we got a rock falling now since the grounds gray you know we might actually might as well give the one oh well just change the ground color so that we can leave the rock as its default sorry my cupboard is being annoying not much I can do about it it's a bit windy I could shut my windows if you don't mind one second okay so what I'm gonna do now is we want to make a we want something to happen when it collides so we're going to go onto our rock in the scene we might as well make it a prefab so that we have because this isn't the prefab of the rock this is the actual blender file so we'll just call it rock full call it whatever you want and it will have Rock shatter as well now let's just call this as well like rock full and we'll add some code to this so we're going to add a script call like you know rock shatter there we go we get our script on the rock and obviously when this is compiled then we can open it up in a visual studio and all we want is when it collides with whatever we want it to shatter into oblivion when I shot it shudder it into pieces so we'll say void well actually what we're gonna do is we're gonna make it a trigger Collider and the reason is because we want to know when it more we could do on Collider and we'll do trigger because I feel like we want as soon as it hits as soon as the rock triggers with any object we want it to then split into pieces which will then have a not trigger there'll be there'll be actual collided so if we say void on trigger enter and we pass in a Collider called other which is the way they normally do it now this is what's going to get ran when the rock you know goes into something so we want to this is why we want to do the whole like you know Rock shattering thing we're not actually gonna shatter a rock we're just going to swap the model women of Iraq so we need to take in a public game object rock shattered and then when we when it collided something we want to instantiate Rock what to instantiate rock shattered and we want to do it at this position so where we are right now and we want to do it at the same rotation so if you've paused it frame by frame and you went to the next time you wouldn't know it's any difference it's just switching out the rock with an identical copy of itself but the only difference about the other one is that it's split up into pieces the reasons why the reason why you don't start off with a thing that splinter pieces is because it would just you know fall to pieces quite instantly so this is going to just basically make the shattered rock at our location but after we've made that rock our location we actually want to destroy this object so we get rid of this game object now this is very this isn't the code Ivan my thing in mine I have it so that after it makes the other rock or even before this it deals all the damage and adds four sounds knock-back then it does this you don't want to like destroy the object then put any code out for this because it'll be destroyed so what we want to do now is we want to have our actual shattered rock to instantiate and then you know get Ridley of one so what we're going to do is we are gonna go back here and you'll see that now on the script it all want to take in and game object which will be the shared rock so let's let's go into blender with our rock oh one I've already got blender open but yeah we want to open the rock so as you see here let me just close this we now have our rock and we want to split this mesh up assuming you've use blender before hopefully we want this is a you know if we move this edge it's actually like all connected you know all of these nice adversities are in on edge all of these parts are still connected you can't just like go inside of it and you know they're all connected we want to have multiple different meshes as different fragments of the rock now you probably could do this by hand but it would take years so luckily blend that includes an add-on which you just have to enable you'd have to download anything which allows you to do it so we'll show you but firstly you want to be in object mode this is edit mode where you can see all the vertices and edges and faces you want to press tab and it takes you back to object mode and you can see it down here you're in object mode now we're going to go to file user preferences add-ons and you'll get a list of all the add-ons that already with blender that you haven't gotten abled we want to search for cell and you'll see we have one called object cell fracture so if you tick the box and close it make sure you're in object mode the one where you go an orange outline and not this you're in object mode you want to then press spacebar when you've got it selected so not there's only one object is selected like this press spacebar for the search and you can search cell fracture there your object cell fracture press enter and you get this window now I've never whenever I've done this before I've never changed any of these settings I don't need to you can obviously tweak stuff if you want to so what you want I'm just gonna recommend you know pressing ok and what it's going to do is it builds as you see they're randomly fractured pieces of it basically builds a box around your thing then it uses the boolean tool now if you don't know the boolean tool is it's a modifier and it literally if you look now whether so this is the decimated I already had on it you'd have to worry about that that was just for making the object look low poly the boolean tool basically makes it so that you can cut out parts of an object where other objects overlap it so if you put a modifier a boolean on to a like cylinder and you shove the cylinder inside you pressed apply and you removed it there'll be a giant cylinder hole through it so that's what you can do for modeling that kind of thing now you might think are objects normal but if you look down here didn't actually do it I think I undid sorry I'll just do it again I pressed undo so there we go now if you see down here we have two layers we have our layer here everything's fine and we have a second layer where everything is the same model but it's got lines through it and it's all fractured all little bits of it you know so we could pick a pot and take it out and there's no missing faces it's all acted properly so this object has now just you know fractured up randomly which is more realistic now all you have to do really is you just want to take your new project thing you want to you know save as what am i calling this one I call it rock shutter yeah rock shutter assets what to call it rock oh one shattered blonde and now you can close it and what we're actually going to do is the rock that we had earlier just give me a second it's gonna go into we're gonna actually get rid of that one because we don't even need that now we've just got a new shattered one and the reason why we only need this is if we go to animation and we wait and we put it in the scene you'll see that inside of it we have ICO sphere then all of these cell parts of it so it's split up into like 41 parts so the first eco sphere is just our rock and in all if one's the fragments of a rot of the rock so if you look here we will take our thing to the side oh wait why is this not with everything else good so if we move everything up this is everything this first one is actually just the exact same identical thing of it just without the fragments and then this is obvio the fragments so what we're gonna do is we're gonna pull this back to zero we're gonna take it outside to ruin the prefabs been here we'll call this you know rock full like we had originally and in this whole thing here of all of the cells can be called rock shattered so you'll have you know your original thing all together owning of a charred version and all you need to do is you need to make them both prefabs so I'll replace the old one and I'll put rock shard as a separate one on rock shattered you want to go to all of the cells not not the parent object you want all the separate cells select them all you want to add mesh Collider convex anyone had with rigidbody with a very low mass probably for this because it's just that each this is the mass of each love it to have some point free and I'm not gonna do the math to work out you know the map the actual total weight so I'm just going to put this back to what it was so we had a mesh Collider which was a trigger remember they've won those weren't triggers these rock pavements they're not triggers because they they want to Collider things properly then we had a rigidbody of a mass of five and we had the code called rock shatter now this game object the rock shattered the thing we're instantiating is gonna be this so we're gonna drag it in its prefab we can now delete it from the scene and you know just apply prefab now if we move this up this time we've been waiting for well to go see if it works and there we go we've got a problem okay so the variable rock shattered to Badou has not been assigned okay so it's saying basically yeah I can't instantiate this did I did I not put it into the thing what I swear I did hmm that's odd I swear I did it go back and watch to make sure I did it but anyway let's try now okay that worked the only problem is they kind of all stayed together they could have fallen apart but there was no real like reason for them to because it kind of nicely just fell to the ground so one thing you can do actually once you instantiate it before you destroy we can do we couldn't basically add an explosive force but so we can say how we're gonna do this force because normally you want to apply the force to an object so we could just say getcomponent rigidbody dot explosive yeah al explosion force and we want an amount of force so I don't know 200 vector free so the position is gonna be our transfer sorry the current position so the center of the rock transferring opposition and then the radius we'll put 5 because this is the only object in the scene wasn't matter you my honour too weak to that value that should work because what's gonna do is when it's instantiate all the new ones it's going to add an explosion force around it before hmm was that just not enough force cuz in mine I first tried it 500 to make it seem good why are they not Oh what these fingers don't actually have rigid bodies on them that's quite odd there probably have worked right away then that's quite weird to be honest mesh Collider I must not save the prefab sorry rigid body and point for something I feel like they're gonna go absolutely flying now because I'm I need a really large force to things that weigh that much but there you go there we go that look much better now so if you didn't get what happen that we the rockfalls and as soon as it touches something the big rock it's then gonna create all the shattered pieces of itself in the same place add an explosion force which just means you know a force in that point in the world and it like explodes outwards so all the rocks will go away and then we know destroyed the original rock so we're left it just looks like our rock is shattering when it hits something and you can do this if anything if you want it obviously not to have an explosion force you might want it to just like hit the ground and crumble then you would just take out this line here so you can tweet this to how you want but I just thought it's a really cool thing to do in the I really like how it makes the spell feel in the game so obvious it no do that not that still just is what happens when they fall like without an explosion force supposedly yeah well anyway so you go we've got all these shattered pieces they've all got the rigidbody mesh Collider now this if you do loads of this then obviously all build up in the scene like the M if you have all these objects in the scene you can get more and more so what you might want to do is on these little shards of rock you might want to have a way of them you won't have a time out a co-routine on the code on the little shattered pieces saying like you know after certain hour seconds despawn or whatever so yeah I think that's it to be honest if there's anything else you want to ask then you know feel free to ask and I could make a part two if there's anything more you want to want to know if you see anything you like in any of my videos or in anyone elses videos on unity on the internet and obviously feel free to ask and if I can help I will I hope you like this video thing is a pretty cool one to make I really like this effect and you know it's pretty simple to do all you need to know is very simply how to use blender and you know have unity ready so yeah if you haven't already obviously like the videos that you want to see more of so I know subscribing would be lovely check out our disco channel in the comments to get you know help on anything you need help on also then we have showcase channels to you know show off the work you've been making and just discuss and help each other you know feel free if any of you wanna join and actually be quite like an active help oh you're not you're not really signing up to anything but you can have the special role which is basically for helping people out so they know they can come to you to help the service currently got over 100 members now so you know it's starting to grow but yeah I think I've said everything obviously thanks for watching the video and goodbye
Info
Channel: Dapper Dino
Views: 10,842
Rating: undefined out of 5
Keywords: Unity, Cool, Effect, Shatter, Explode, Coding, Programming, Game
Id: r7mum9teFnk
Channel Id: undefined
Length: 17min 46sec (1066 seconds)
Published: Fri Jun 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.