Awesome Easy DESTRUCTION in Unity! (Add SECRETS!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm your code monkey and here let's learn how to make some simple but awesome destruction so we want to have a wall that when we damage it turns into pieces and it falls apart this is a great simple way to add some awesome secrets onto your games we're going to use the third person shooter controller that was made in a previous video in order to shoot at the wall and damage it and then we're going to apply the same destructibility to some crates and any other objects easily design your next game with this video sponsor millinote miller note is an awesome tool for organizing your projects in freeform virtualboards if you've been watching this channel for a while you might have heard me say that i usually tend to design on paper and the reason is because i can write anywhere and draw some images it's a much better format than a basic text document but paper also has its downsides well what melon does is it provides exactly that type of format with tons of features to help you design and organize your ideas so you can place down images text checklists videos organize them in any way you want make some arrows position all the elements in a way that makes sense organize them into multiple boards and tons more you've got as much space as you want to fit any design id you have from very small to some massive games there's also a ton of preset templates for game design characters world building and lots more really great to get you up and running quickly note is really easy to use and they also have a mobile app so you can easily iterate upon your designs even while you're outside there's a completely free plan with no time limit so click the link in the description to try it out and use it for your next project okay so the way we're going to build this is actually quite simple we just make a normal wong then we make some sort of health system for it then we're going to make some wall parts and when the wall takes enough damage then it automatically changes from the wall into the wall parts and using physics we apply some force in order to smash those parts then we just make it work with bullets and everything looks great okay so let's do this now first of all here is my very normal wall so this one and this whole demo scene this is taken from the polygon cinti heist asset pack there's a link description if you want to get it it's a pretty fun pack for making some sort of heist game okay and it's got some really nice walls now over here the first thing that we need is to make some wall parts and for that we're going to need to break the model into various pieces and in order to do that we can use unity pro builder this is a super useful tool for doing 3d modeling directly inside of unity it's also one of the tools that i cover in my ultimate unity overview course so go ahead and get it in order to learn about pro bundler and 40 other tools and features now over here with my wall this is the normal mesh so this one was probably made in blender or some 3d program like that so the first thing i need is to convert it into the pro bundle format so i just go into tools pro builder open up the pro bundle window and over here just select the object and probe build rise okay so now i have a pro bundler mesh with this we can now use all the probe builder tools to work with this object and specifically we can use the programmer tool that i covered in a previous video it lets us make all kinds of actions with propeller meshes specifically we can use it to cut off parts of a mesh so i can just make an empty 3d cube then on this cube just position it roughly like that let's maybe push to the side put it something like that right on the corner there and now by going into tools pro builder and open up the boolean tool now i can drag the first wall and then my cube and get the intersection of both of them apply and yep over here is my nice corner again go watch that other video if you haven't seen how to use this boolean tool this is a super useful tool that is actually surprisingly hidden okay so now all it takes is do the same thing so just move this into a different position rotate it differently and cut it up again and so on to get all the various parts now over here i have a wall where i did exactly that so i just went through and cut up all kinds of pieces from the wall so i just did that same process over and over again slicing everything into a nice bit of randomness okay so with that now over here i've got two objects so i've got the normal wall and then the wall broken apart now the one that is broken apart is made up of multiple objects so you can see inside this prefab there's all the individual parts and now the one thing that is very important is to make sure that the pivot is always exactly on the same position so over there on the wall the pivot is over there on that corner and for the wall broken it is also on that corner the wall broken will be placed exactly on the same position as the original one so that's why they need to match okay so now here just make sure that i add a collider and a rigid body so first a mesh can leather into one of these parts make it convex and then also add a rigid body so just like this let's see if all the meshes do have proper collisions and if there's the wall and yep they do have proper collisions and on the third-person interior that i made that already applies them for so if i shoot if i can destroy this whole wall alright so far so good by the way if you find the video helpful please hit the like button it's a tiny thing but it really does help thanks so we have all the assets that we need now let's end the logic for how to use them like i said we're going to make the wall damage among but i don't want the wall to be destroyed with a single hit so let's make the wall have a proper health system and the first thing we need is just a script so in uc sharp script called destructible wall then on the normal wall object let's just attach a script onto it okay now let's open so like i said we're going to need a health system for our wall and as a great thing over here i'm going to reuse the health system that i've made a really long time ago so this was actually made in the very first video on this channel so just private health system make it of type health system and on private void awake just create it there you go again this class was made on the very first video on this channel over three years ago so this is an excellent example of how you should build nice clean classes that you can then reuse and save some time many years in the future okay so with this we've got a health system now for dealing some damage over here i have the third person shooter controller that was made in a previous video and then down here for detecting hits it uses a raycast so it identifies it sees if it has a rigid body if so it applies explosion so over here we can test if the collider contains some other component now we could write some code here to make it work specifically by testing for the destructible one component but instead let's write some nice clean code so instead of testing specifically for this destructible wall let's make it work with a simple damageable interface so let's just make a new c sharp script call it i damageable and then over here this one is not a class but rather it is an interface and it is not a model behavior if you are not familiar with interfaces i cover them in more detail in another video so go watch that if you want to learn more they are super useful for making your code extremely adaptable now for the functions let's just add a void damage function we're going to receive an end for the damage amount and that's pretty much it that's the only function we need right now so over here when we do the raycast let's do the same thing that i did over here for the rigidbody but instead do it for our interface so for the i damageable interface so just get that interface and then go and simply call the damage function then pass in the damage amount let's say i wanted one to be destroyed in three hits so maybe 33. okay so now the third person shooter should already be dealing damage to objects with eye damageable so now we need to do is go into our destructible wall and over here we extend mono behavior then we implement our interface and let's implement it and over here on the damage just go into the health system in order to cause some damage so just deal damage passing the damage amount and now this health system actually has a bunch of really nice events specifically it's got an event when it dies so we can subscribe to that so the health system subscribe to the undead event and when the undead event happens then let's just destroy this object this game object and just do a debug okay so just like this let's test and see if we can damage and destroy the wall alright so here i am and there's the wall one two three four and there you go the wall gets destroyed all right awesome so now we just combine this with our wall parts so on the one script let's set a reference to our prefab and then when we destroy and let's call instantiate we're going to instantiate the broken wall prefab instantiate on this transform position and this transform rotation okay so let's test so here i am let's aim and shoot and there you go it does destroy the wall and the spawn d1 parts all right awesome and again just like this like we saw previously since the bullets are automatically causing some force onto all these objects i can already this right along so it already looks pretty great but we do want the wall to fall apart pretty much automatically as soon as it's spawned and we also want to hide that transition with some vfx so let's do that okay so for the explosion force after we spawn the walls we want to apply some explosion force now for that that means that we need to know the position of the explosion so let's pass it in on our damage function so let's modify our interface to receive the damage amount and also a vector three for the damage position over here let's do it factor three for the damage position and then on the third percent shooter let's trigger with that damage and then the position will be on the raycast hit that point so this is the exact point that we hit okay so with that now let's just store the last damage position okay so we've got the unless damage position then using this we can now apply some force now to apply the force we need a reference to some rigid body so let's cycle through all the children and then over here we can call add explosion force and we're going to use the last damage position and let's just also spawn some vfx so let's make another one and we're just going to call instantiate spawn of the effects directly on the last damage position okay that's it let's test okay so here i am aim at the wall and shoot and if there you go the wall explodes and boom it falls apart instantly alright so we can play around those valleys a bit more alright so here i am there's the wall aim and shoot boom and there you go all right so the wall gets destroyed now i can go in right awesome so with this we have everything working now again of course this whole thing can be applied to also really just about any object all right so i cut up the crate into various parts so now to make this code more reusable let's rename the class from being destructive along to being destructive on something so maybe just destructible object make this work with anything and again since the shooter code already works with the eye dimensional interface then we don't need to modify that one because it doesn't deal directly with this object now i can also expose the health amount and also expose a float for the explosion force and then just need to attach the object so there you go actually create broken the same vfx the health amount max let's say i want this one to be destroyed in just one hit so let's switch is 30 and explosion force yeah maybe 200 let's see all right so here there's a nice crate and as soon as i shoot it boom it explodes and goes into all body parts and the wall as well boom explodes all gone alright awesome all right so here with a whole bunch of crates on the wall and as soon as i destroyed boom they all fly they all explode they don't get destroyed lots of particles everything flying everywhere all right awesome so you can see how you can use this to add just a bit more life to your games and make some descriptive rolls in order to hide some secrets or really just add a whole bunch of description here to make your whole level feel much more alive also don't forget to check out melo note with the link in the description it's free so check it out and try using it in your next project alright hope that's useful check out these videos to learn some more thanks to these awesome patreon supporters for making these videos possible thank you for watching and i'll see you next time [Music]
Info
Channel: Code Monkey
Views: 49,103
Rating: undefined out of 5
Keywords: unity mesh deformation, unity destruction physics, probuilder experimental, unity mesh destruction, unity slice mesh, unity destruction, probuilder boolean intersection, unity slice, code monkey, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 3d, unity, game development, game dev, game development unity, unity 3d tutorial, unity tutorials, how to make a game, unity probuilder, unity mesh
Id: tPWMZ4Ic7PA
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Mon Aug 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.