GRENADE / BOMB in Unity (Tutorial)

in this explosive video we’re going to create some bombs and grenades in unity towards the end it should look something like this let’s just jump right into it so normally hand grenades can be split up into three parts part one means throwing the grenade but two is waiting X amount of seconds during which the grenade can bounce off the environment and part three years of course an explosion here we both want to create some kind of visual effect add forces to nearby objects as well as damage them so these are the three steps that will be following during this video let’s start by focusing on the second one cerise assemble scene with some crates and some bottles from the western props pack if you want to use the same model so you can download them for free by using the link in the description so let’s go ahead and add a grenade to our scene I’ve created a very similar grenade model in blender please don’t make fun of it you can of course use any model that you’d like I’m just gonna take mine here and place it into the scene I’m also going to drag it up here and place it somewhere in the middle of the crates now we want to apply physics to this grenade so let’s go ahead and add a new component and the first thing that we’ll need is a Collider I think a capsule Collider works pretty well here if we open this up we can of course adjust the radius let’s shrink that down a little bit we can also adjust the height and you can see this goes in the wrong direction so I’m gonna adjust this from Y to Z and now we can fit this to the grenade so I think that’s close enough and the next component that we want to add is of course a rigidbody so now we should see that if we rotate our object over a little bit and hit play it’s going to fall and land on our crates of course I’ve also made sure to go ahead and add a rigidbody and a box Collider on each of the crates as well as the bottles you’ll also notice that there is currently a destructible script sitting on each of these objects this is in no way required and we’ll talk about why it’s here later so now our grenade is physics enabled and we’re ready to give it some logic let’s select our grenade let’s hit add component and let’s create a new script called grenade let’s like C sharp and hit crane ad let’s double click the script open it up in Visual Studio and the first thing that we want to add at the very top of our script is a variable that will control the delay before Ocwen aid explodes so we’ll create a public float we’ll call it delay and set it equal to about 3 seconds and whenever we want to delay an action through script there are multiple ways to go about it one that I think works really well and that it’s also fairly simple is by using a countdown variable so we’ll go in here and create another float this one doesn’t need to be public because we don’t need to change it manually through the inspector we’ll call it countdown then in our start method will set countdown equal to our delay so right when this grenade spawns its countdown is going to be three seconds then every frame so inside of our update loop we are going to reduce our countdown a tiny bit we’re going to say countdown minus equals time Delta time remember time that Delta time is the amount of time path since we drew the last frame and if we reduce by this amount each frame we’re essentially reducing our countdown by one each second so then what we can do is check if our countdown reaches zero so if countdown is less than or equal to zero well then three seconds have passed and we’re ready to go ahead and explode for this we’ll create a separate function void explode and this method is where we’ll handle step three for now we can just throw a debug deadlock saying boom if we now save this script go into unity and hit play we can see that our innate false and after three seconds it says boom in the console however currently it’s spams boom a whole bunch of times a second and we really only wanted to say this once to solve this we’ll create another variable at the top this one is going to be of type boolean and we’ll call it has exploded by default this is going to be false then when we’re checking if our countdown has reached zero we also want to check if has exploded is equal to false and we can write this in shorthand notation by simply putting an exclamation mark in front of has exploded so now we only call explode if we haven’t already exploded and then right after we explode we said has exploded to true now if we save this and hit play once more we should see that after three seconds we only get one message in the console awesome so now we’re ready for step three I’ve been looking forward to this one now for this step will replace our message with some actual logic now this step can also be split into multiple parts and you might not want to have all of them depending on your game the first part is showing some kind of explosion effect the second is finding all nearby objects once we have these objects we can add forces to them in order to make them move away from the explosion as well as damage them and finally we want to get rid of our grenade so the first and last step here is actually really easy to show a cool effect we simply need to instantiate some particles to do that we first need a reference to the particles that we want to create we’ll create a public game object and we’ll call it explosion effect then when we want to show the effect we simply instantiate our explosion effect and we’ll do that at the grenades current position and rotation so we’ll use transform dot position come and transform dart rotation and that’s really all we need to do here the last step is also really simple to remove the grenade we’ll simply called destroy and then we’ll insert our game object which is to create itself so now our grenade should visually be working if we save this go into unity and select our cüneyt you can see that we now have a slot for our explosion effect now I’m using the unity particle pack which is free of the asset store I’ll of course have a link for this in the description and one of the particle effects that you get here is a big explosion effect so now if we hit play and wait the three seconds we should see that we get a big explosion effect and that the grenade disappears from both our scene and our hierarchy but our surrounding objects are still completely untouched to change this that start by getting our nearby objects and unity actually has a really useful method for this it’s called physics dot overlap sphere this allows us to define a sphere at a given position with the radius that we choose and then unity will give us a list of all the objects that are colliding with this sphere so if we input the current position of our grenade transform drop position as well as the radius that we want and we can go up here and create another variable for that we’ll create a public float blast radius or assembly radius I will default this to about five units so now we can insert that down here so we’ll insert our radius and now this method will return an array of all the colliders overlapping with the sphere we now store these colliders in an array so we’ll go Collider array and remember an array is basically just a list of items with a fixed length we can call our array colliders and set it equal to our function now we can go through each Collider so we’ll use for each Collider the college Collider that we’re looking at nearby object so for each nearby object in our colliders array we’ll both add some force and some damage and let’s begin by adding some force to do this we’ll get a component on the nearby object that we’re looking at so nearby object dark get component and we want a component of the type rigidbody will then store this in a variable so we’ll do rigidbody call it RP and set it equal to the component that we find however not all nearby objects are necessarily rigid bodies so we do have to check here if RB is not equal to null in which case there is a rigidbody on the nearby object and therefore we can go ahead and add a force so we can write R be dot ad and the mistake here would be to just go add force we’d have to calculate a direction as well a magnitude of the force depending on the distance between the current object we’re looking at and the grenade and yadda-yadda-yadda of course this can be done but a much simpler way is using the built in ad explosion force that’s right as always unit is created an awesome function to help us do this this will do all the appropriate calculations for us all we need to do is simply give it an explosion force and we’ll of course make another variable for this at the top so we’ll do public float force and we’ll default this to something quite high so we’ll do 700 and then here we’ll write explosion force as well as the origin of the explosion so that is our current transformed position as well as an explosion radius and here we’ll simply input our radius and whoops I wrote explosion force here recently named it force so just to go through this quickly we are getting all of the nearby objects we are looping through each of them we’re searching for a rigidbody on each object and if it has one then we add a force pointing away from the grenade if we now save this go into unity and hit play on top of the usual particles we should see some cool forces being applied to the environment however all of our objects are still completely intact what we need is a cool way of destroying these luckily I’ve created a video on this subject using the same models it’s a fairly easy effect to achieve but it can look super cool and that’s also why we have a destructible script sitting on each of the crates and bottles now let’s try and open this up in the video on shattering we set it up so whenever we click on an object it’s going to get destroyed now we can really simply tweak this script to work with our grenade so instead of waiting for the player to click on the object we’ll simply make the method public so we can call it from our grenade script and we’ll rename it to something simple like destroy if we then save this and go into our grenade script we can do pretty much the same thing we’re doing with our rigidbody but with our destructible script instead so first we use nearby object getcomponent to get our destructible component we then store this in a variable of type destructible we’ll just call it test and set it equal to the nearby object we then check if test is not equal to null in which case we can call the destroy method that we just created if we now save this and try playing we should see that after three seconds our objects get destroyed however currently all of the pieces just land on top of each other that’s because our forces aren’t being applied to the individual pieces and since we achieve this effect by replacing the original objects with a bunch of pieces we aren’t actually applying any forces at all so instead what we need to do is split this for each statement into two the first one is going to destroy any objects and the second one is going to add forces so we actually need to search for colliders twice the first one will search for colliders to destroy and the second time we’ll search for colliders to add forces to so we’ll rename this variable to colliders to destroy let’s now copy this line and paste it down here and this time we’ll call it colliders to move we can then create a new forage statement where we again want to look through all the colliders we’ll call each one nearby object just like we did before but this time we want to look through our colliders to move instead of our colliders to destroy we can then take off the logic that add forces remove them from the first for each statement and instead paste it to the second one and now our code should make sense we find all the objects that we want to destroy and destroy them and we find all the objects that we want to add forces to and we move them let’s try and save this and this time we should see the effect working and indeed we do man I love explosions so that’s it for step two and three of course we still need a way to throw a grenade now this step can be done in a bunch of different ways and it’s going to completely depend on the game that you’re making in this case we’ll imagine that we’re creating a first person shooter and we’ll throw the grenade toward the center of our screen to do that we’ll select our camera we’ll add a new component and we’ll call it grenade thrower here we can remove the start method and instead create a public float which is going to be our throw force we can default this to something like 40 then inside of our update method we want to check if we’ve pressed the grenade key in our case let’s just use the left mouse button so if input dot get mouse button down the button that we want to check for is the left mouse button which is zero and if we press this button well then we want to throw a grenade again we’ll create this in a separate function void throw grenade and here we first need to instantiate a grenade so again we’ll go to the top and create another public variable this one of type game object and this will be a reference to our grenade prefab let’s save this and go into unity we need to create a prefab from a grenade and let’s also just adjust the force down to maybe 200 when the integrin aide and drag it into the project panel and we can now see the prefab here let’s remove it from our scene and instead select our main camera where we can now drag in our grenade into the grenade prefab slot so now we can simply instantiate our grenade prefab at the current position of our camera and using the same rotation as our camera of course this script doesn’t need to be placed at your camera’s position we could place it on any object pointing in the direction that you want to throw your grenade we then get a reference to the newly created grenade to do that we’ll create a game object call it grenade and set it equal to the grenade that we just instantiate it we can then apply a forward force to our grenade to do this we need to get the rigidbody so again we’ll go rigidbody RB and set it equal to grenade dart getcomponent of type rigidbody and we’ll just assume that the grenade prefab we’re using has a rigidbody so we can simply call RB add force and here we give it a direction the direct of course wherever our camera is looking so in our case that would be transformed forward we can then multiply this with our throw force and there you have it now whenever we press our left mouse button we call throw grenade which is then going to create a grenade get the rigid body on that grenade tell the rigid body to add a force in the forward direction and to adjust the force depending on a throw force and the last thing that we need to do here is change the force mode because this is not a force that we apply over time and because we don’t care about the mass of our grenade will change the force mode to velocity change if we now save this and hit play you will now see that if I press my left mouse button we throw a grenade and after three seconds it explodes of course the force might be a bit too much here so let’s try 20 instead and you can really go nuts and throw as many grenades as you’d like so that’s pretty much it for this video if you enjoyed it make sure to subscribe so don’t miss the future one also definitely check out the video on destructible x’ if you haven’t already on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in October and a special thanks to Judy Minh Armand pence off-tune Cobra Superman the great James P Thomas Wally Sybok mummy Jason the Tito Derek Ames Kirk fatal Murphy Manolis Nick Lang Erin Robert Bund and Peter Locke you guys Rock

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


Leave a Reply

Your email address will not be published. Required fields are marked *