Destroy Objects and Fade Out the Pieces | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
blowing up objects is always a lot of fun in this video what we're gonna do is take a look at the most simple way you can implement destroying objects in your game hey chris here from mom academy here to help you yes you make your game dad dreams become a reality this technique we're going to look at today is the most simple way to do it destroying objects there's a bunch of different ways there's a bunch of assets even on the unity asset store that allow you to do this in a more complicated way but this way is absolutely free as long as you know how to do some kind of modeling concept of what we're going to do is pretty simple we're going to have one version of the game object that is our fully combined normal model that you would use in your game and the second one is going to be one that you spend a lot of time splitting it up slicing it dicing it so it looks like it's destroyed and you have a bunch of sub pieces it's really important this is not a single mesh because we're going to attach rigid bodies to each of these sub pieces for the destroyed variant so that way they can explode out and go somewhere else we're going to do to actually perform this destruction is disable the collider and the render of our combined normal mesh and replace it with at the exact same position that one that we've sliced and diced and made a bunch of sub-pieces for then we can add an explosive effect to make it kind of go and the rigid bodies on all those sub-pieces will interact with the world and look pretty nice depending on how well you made your destroyed mesh after we've destroyed this other mesh and all the rigid bodies have kind of settled wherever they're going to be we don't want to leave them in the scene forever because that's a lot of extra performance being wasted on all these rigid bodies there's some overhead with just having the rigid bodies there so what we're going to do is wait after they've all settled and then we're going to have them go down into the world and then we're going to destroy them once they're off screen this is a really common thing i've seen in a bunch of different games aaa titles even do this the player normally is not going to really notice that these are disappearing they're usually going to notice that oh they're gone unless they're really looking for this because normally they're going to be in the middle of game play and having them subtly just slide out is a pretty nice technique to use hey and just really quick i want to give a shout out to my patreon supporters i really appreciate it every bit helps the channel grow reach more people and add value to more people and that means more people are making their game development dreams become a reality if you want to help me in that cause you can show your support on patreon patreon.com academy you can get your name up on the screen you can get a voice shout out starting at the silver tier and some other cool perks special shout outs to rafael and andrew bowen for being the silver tier supporters i am so grateful thank you let's take a look at how we can set up our game objects where we have a non-destructive version and a destructed version i'm going to use a pro builder cube but you can apply this same principle to any model that you want so i have a cube here and i also have a prefab that i've pre-destructed because honestly it takes quite a bit of time to blow up a cube i probably spent a good hour just slicing up the cube and making it work the right way i am not a particularly good 3d modeler so if you are you can probably do that much faster i have this cube in a broken cube and they're stacked exactly on top of one another so this one cube has the six faces and a broken cube if i select that you can see has approximately 40 sub pieces all of which have six sides one really important thing is that every single broken piece needs a rigid body that is not kinematic and has a convex collider on it or a simple primitive type if you want to use a box collider or something like that that's also fine but if you're using a mesh collider which is the default if you're slicing up probiolar cubes you need it to be convex setting the mass of the rigid body is also important i set them all to be one which is the default the mass that you set will affect the explosive force we need to have them blow up whenever we press a button now let me just go really quickly and show you how you can slice up a cube to achieve this result you can do this with most models because pro builder has a feature called pro builderize which converts a mesh into a pro builder object this then lets you use pro builder to slice it dice it and move it around if you know how to use something like blender maya autodesk any big name 3d modeling program it's probably much easier and faster for you to just make your models in there i have almost no idea how to use blender so that's why i'm doing it here what we can do is click insert edge loop that gives us one of these cubes that goes perpendicular to the edge that we had selected we can rearrange these and slice it in every direction then if we click this little three dot thing that's the vertices selection and then i'll just move these around a little bit so that way they're a little bit more random it's not a perfectly sliced up cube and then we can go and take i'm gonna start on the corner select the three faces on the corner with the face select tool and click detach faces what this does is splits off those faces from the object that we were just looking at and then what we can do is fill in the missing edges by either extruding or if you select two edges and press for me it's alt b which is the same as pressing connect edges and it will make a triangle out of your selected edges you can then do this so that way we have a fully enclosed shape again and repeat that process for the entire broken object that's why it takes a long time enough about that if we come back inside of this little room thing we can see i have a bunch of stacks of cubes i have this cube as a prefab where i have already attached a destructible clip that has an audio source attached to it because we want it to play some sound whenever it blows up and they all have a rigid body on it because i want them to be able to fall or get pushed by other rigid bodies i'm once again using the starter assets third person character controller from the asset store it's a free asset provided by unity that gives us movement and some default inputs with the new input system i've also attached a player input script we'll look at that a little bit later the important thing here is that the player input from the starter assets has the behavior send messages in our player input script that we've made is on that same object and if we look at the starter assets input actions we can see that we have already added an action called use it's bound to the e button and because the player input has send messages set up there's an on used message that will be sent to our player input and that's where we're going to handle raycasting to tell if we're going to blow up something or not if we open up visual studio we'll start with the destructible class at the top we'll add a required component type of audio source and add a private rigid body rigid body and a private audio source audio source we're not making the rigid body required on this but we do want them to always have to play a sound whenever they are blown up we'll add a bunch of private serialized fields a game object called broken prefab that'll be the prefab that we're going to swap out whenever it blows up an audio clip destruction clip that'll be the clip that we play whenever we blow it up then some physics ones a float explosive force set that to be 1000 by default explosive radius 2f by default these two will control how the explosive force works whenever we blow up this object then we're going to add some variables to control how we're going to fade out the blown-up objects after they've settled for a while because you do not want thousands of rigid bodies running around in your scenes that's really bad for your performance the piece fade speed a piece destroy delay and a piece sleep check delay on awake we'll grab the references to rigid body and audio source with git component rigid body and audio source respectively and then we'll define a public void explode first thing we'll do is check if the rigid body is not null if it's not we're going to destroy it because we don't want to move anymore we're also going to do if try get component collider out glider collider collider.enabled equals false and we'll do the same for the renderer we're doing it this way to be a little bit safe so that way we don't end up with a null reference exception because possibly there's no render or collider on it one other thing to consider here is if you're using a destructible object that has multiple sub-renders or sub-colliders you may need to do something like this where we grab all the references to colliders and renders in the children and iterate over those and disable them finally we'll check if the destruction clip is not null we will do audio source dot play one shot destruction clip that just makes the audio source play one time this clip we'll then do game object broken instance equals instantiate broken prefab at the transform position of this object and the transform rotation of this object that way it's a seamless swap out between the non-destroyed version and the destroyed version we'll then grab references to all the rigid bodies in the children of that broken instance with broken instance.gate components and children rigid body we'll then iterate over all those rigid bodies with 4h rigid body body in rigid bodies and check if rigid body which remember is the rigid body attached to this destructible object if that's not null what we're going to do is inherit the velocities of all of the rigid bodies so that way they're moving in some direction whichever way this rigid body was moving with body dot velocity equals rigidbody.velocity then we're going to add the explosive force with body.add explosion force with the explosive force we've defined at the transform dot position so that's the center hopefully of this destructible object with the explosive radius we're then going to start a curve routine to fade out the rigid bodies passing in that array of rigid bodies let's go ahead and define that with private eye enumerator fade out rigid bodies that accepts an array of rigid bodies called rigid bodies in here we'll define a weight for second's weight equals new weight for seconds with that piece sleep check delay do an int active rigid body equals rigid body to start length and say while active rigid bodies is greater than zero will yield return a weight we're immediately returning that weight because we're adding the explosive force most likely in the immediate next frame all these rigid bodies are moving so we're going to skip and wait until they would realistically possibly already be sleeping we'll then do for each rigid body rigid body in rigid bodies we'll check if rigid body diet is sleeping if that's true we'll do active rigid bodies minus minus and that'll loop until again we have no active rigid bodies once that loop has ended we will yield return new wait per second piece destroy delay we put 5 by default and then we will fade all of them out so we'll define a float time equal 0 and get references to all their renderers on these rigid bodies by doing render array renders equals array that comes from the system name space dot convert all rigid bodies get renderer from rigidbody let's go ahead and define that converter with private renderer get renderer from rigidbody that accepts the rigidbody rigidbody and what we're going to do in there is return rigidbody.gitcomponent renderer we're then again going to iterate over all the rigidbodies with for each rigidbody body in rigidbodies and we're going to destroy the rigidbody collider and the rigid body since we're going to fade all these out we don't want the physics system interacting with these objects anymore that's why we're destroying them we're then going to do while time is less than one we'll calculate the step size that we're going to use we're going to do float step equals time dot delta time times piece fade speed and iterate over each of the renders with for each render render in renders we're going to do renderer.transform.translate passing in vector3 down times the step divided by the renderbounds.size and we're going to translate down in the world space so transform translate right just moves the transform and since we're iterating from time 0 to time 1 we're going to translate it some amount based on the bounds of the renderer that we're going to use so there's a bounding box around each renderer and we're going to move it down so it fades all the way down where it'd be into the ground and then once it's done what we're going to do is destroy it once we've gone through all the renderers we'll do time plus equals step and then yield return null to wait for the next frame once we've finished that loop where we've got time up to 1 so all of our renders have been moved down out of view we will go over all the renders and destroy them with for each renderer renderer in renders destroy render.gameobjects that's actually destroying the entire piece not just the render and then finally we will destroy this game object the destructible we're waiting to destroy this destructible game object until the very end because we have this co routine associated with this destructible script which is on that game object so if we destroyed this game object early this co routine would stop running and nothing would happen that should work pretty well for us let's take a look quickly at the player input this is almost the exact same as what we did in the doors video where we're gonna raycast out from the camera some distance ahead of us that's the use distance on some layers the interactable layers and if they hit something we're going to show some text to notify the player they can do something with whatever it is they're looking at that's this explode text block here if they're not seeing anything that is interactable we'll set that text to be disabled earlier i was talking about this on use function that gets called by the new input system because we've defined a new action called use in there we're going to do the exact same physics raycast that we're doing on the update function but we're going to do also try to get the component destructible from that and if it is a destructible object we will call that explode function that we defined in the destructible that actually blows it up if we hop back to the unity editor i'll select the cube prefab hook up the broken prefab my super high quality explosion sound and what i'm going to do is lower the explosive force to 500 i think 1000 might be a little bit high everything else i'm going to leave alone so if we click play we'll see this explode text shows up whenever i'm looking at a cube if i press e it explodes we get some awesome sound effects and all the pieces kind of fly around we'll also see that after some period of time all these objects start fading into the ground and go away we can run around and blow up a bunch of these objects i kind of got carried away sorry blowing up this stuff it was pretty satisfying and if you look at this big mess of broken pieces we'll see that they slowly fade out and go into the floor and then the original object and the broken objects are destroyed one thing that i do see here is you see the broken cube clone does not get destroyed because the parent on this prefab does not have a rigid body on it there's just a container so one small enhancement we could make is to destroy that broken cube root game object so that way we don't have all these extra game objects floating around in our scene if i adjust the explosive force to be something like 100 we'll see that these game objects break a lot less explosively so you can play with these settings to see what makes the most sense for you and your game a couple ways that we can improve this are to either slow down that fading down because it was a little bit fast with the 0.25 you can also do something where you fade out the material so that way it's they become transparent and smaller instead of just sliding down the fade out effect you choose to use is totally up to you we just chose the simple slide down but there's a bunch of different ways you can try to approach this and like i was saying at the very beginning of this video there are other ways that you can do this besides the simple swap this is like the easy fast way to get up and running with it but the downside to this one is that you have to spend a lot of time modeling your destroyed version i personally spent like an hour or two just slicing them to the one cube so i can't imagine if you have like really complex models how long that would take and it's a little bit more limited because you can't have big combined meshes let's take if you have pillars on some big building you can't just destroy each pillar because most likely you've combined that entire mesh together for performance reasons that's where some of these assets like rayfire on the asset store come into play where they have the procedural destruction built in now you don't need to come in and model all the different destroyed versions of every single thing you have it'll at run time slice the mesh destroy it however you want it to go if there's a lot of interest in that maybe i can take a look at how to do that i've never done procedural mesh generation or slicing before so that might take me a little bit to figure out but let me know in the comments if that's something that you're interested in seeing and i'll see what i can do to make that happen if you got value as a video please consider liking and subscribing to help the channel grow reach more people and add value to more people there's new videos posted every tuesday and i'll see you next week
Info
Channel: LlamAcademy
Views: 6,655
Rating: undefined out of 5
Keywords: Unity, Tutorial, How to, How to unity, unity how to, llamacademy, destructible objects, destroy objects, unity destroy objects, unity destructible objects, unity smash objects, unity break objects, unity break, break, destroy, destructible, shatter, unity shatter, unity shatter objects, explode objects, unity explode objects, explode, objects, unity blow up objects, blow up, blow-up, fracture, unity fracture, unity fracture objects
Id: 3OWeCDr1RUs
Channel Id: undefined
Length: 15min 34sec (934 seconds)
Published: Tue Dec 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.