Break the box - using Unity and particle systems

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back let's continue on the platforming theme that I apparently have going on all right this time similar to the coin box instead we will make a breakable box which is when you hit it from below it will break into ball approach and fall down like in Mario when he jumps up and destroys a brick this time we will be using the acid part back film kami it's available on open game art okay then let's jump into unity here I have already prepared a project we have a player and I grounded him move on a plating currently move and jump from this play console script I have imported this price for the brick and I made a sound for remedy swing bridge okay let's go ahead and drag this brick right in the scene is that it's position zero zero move it somewhere where you can jump and hit okay let's see if this is first hit yes you can see that they can jump high enough okay let's give the brick box Collider and a rigid body and set the rigid body static let's see if we can interact with our drink great now in create the script breakable box Oh inside remove everything let's start very simplistic make a one collision method and then uh non-collision story game object all right let's see if you touch a brake it should be destroyed oh we forgot to assign the breakable Box scripted a break a though and now we try again okay it works now let's check if what hit us is the player if other not cola con Collider that game object get component let's see if it has the pagans ona play console is a script that is attached to the player and thus it should not be attached to anything other than a player so if the thing that hits the brick as descript we can assume it is a player in this is I found a better way of doing this check for player than taking for a tag or a name okay let's again see if it works yeah we can still hear it but we can also hit it sighs as well so let's go on and make it so that we can only break it if you hated them below so once we have check that it is a player we can also check and it is commit the hit is coming from below so other not contacts is the context contact points that the collide is getting hit on the first contact point as there was only one hit and then we want to check the normal of that hit and we only want to check it in that y-axis and this values should be greater than 0.5 if it is then we can assume the heat came from below let's save and go into the scene again and tight if we hit this on the side nothing from above nothing below yes it's not great let's move on now when we are destroying the break we want smaller brick pots to follow out of it like in Mario to do that you can actually create a particle system Oh brick wall and create effects particle system and I am no expert in particle system so you can probably make this even better than I am I will just go ahead and write something we can start but in the shape we see that the particles are shooting out from everywhere but the only wanted to shoot out upwards let's go to the shape and change it the circle and then we need to change the ark into perhaps 90 degrees and then we can go up to the rotation and change it to 45 let me see that now they're shooting out up and we also want the bits to fall down so let's and adjusted gravity modifier and set it to two you can see they go up and then a fall down let's adjust the lifetime now the particles are living for way too long we only want them to live a short time so let's eat - perhaps 0.8 that seems enough and we don't want the particles to play very long so set this to you something small 0.1 and we also don't want it to loop so turn off looping now we can go down to the emission and set it bursts as we want them to burst out only once we can set the count to four or five perhaps let's start with five okay this looks good doesn't it let's fine-tune this a bit again not by having a correct sprite flowers we want the particles to look like a brick or a small herb report but for simplicity I will just make them look like the brick itself but smaller to do that let's create a new material great Tyrael can name it greek material and we change it we can sighing aspect it's we had have to ingest new shader we can same beta particles only then to the albedo ready in the brick wall material all right now it looks like a big wall then go back to the particle system down to the renderer and here on the default particle material what and drag in the brick material let's see what it looks like all right but the brakes are way too big at the moment let's go and change the size of them and makes me like make the size very Oh star shines put it random between two numbers left open to and opened for yeah that looks good enough and also let's add a rotation overtime with a look a bit better and also started Asian but with Astoria tation then between twos Constance - 45 - 45 it looks good man okay rotation of a lifetime and set it to a random meeting two constants 32 - 45 - 45 again let's see at this loops I looks good you could have changes to minus 90 90 instead and they get a bit more of a rotation okay I actually think this looks good enough now so let's go ahead and implement this in our code see that the particle system is a child to the brick wall inside of our script let's grab it make enough weight method and create a variable for the particle system private particle system particle and in the wake method particle because you get component in children remember it's in the children type particle system and then instead of destroying it here let's create a method mister great you can hold it let's create this method and and set this to a game object and also particle not play right before dying you got the particle effect but will this really work let's go inside when you play oh let's go back to the particle system and make sure you uncheck play on awake like that go back inside the game okay and now if you jump nothing happens it only disappears that is because remember the particle system is a child of quick war and we are destroying the brick wall as soon as we are breaking this box the particle system doesn't have time to finish before box get started I get soiled we need to make this and I enumerate a enumerator instead and call it a cover team team did I spell it right yes so all right then after the particle system we will do a yield returned wait four seconds and how long should we wait well we can check how long the particle system is and wait for that long portico not main lifetime start lifetime constant max max this is the longest time one of the particles will live take that time wait for it and then we destroy area our game on right yes there you go but you can see that the box still remains until at the source so what we need to do is to disable the sprite lender if you do that we disable it you can see that the spider anger disappears let's do that by code the reference to display tender private right yeah because we get component right and then right after we have played the particle take the spider and set the enable to false now if you play and see that the box should disappear when we hit it perfect that's what we wanted but there is still one issue what happens if we hit the box a second time before it has been destroyed decided to make a double jump if we could hit it two times which is because the box Collider that the box has is still available for us to be but as to hit we also need to disable that one we can do it the same way as we did with the spider and with Collider yeah like so and in the same way as we said despite sender to disable we do the same with that box kalila enable equal to false now if it's either double jump again we should not be able to hit it the second time perfect well it's just as we want it now so we only have one more thing to do now is to add the sound let's go to the brick wall and add a component and audio source like that and then we can assign the clip music file to the clip and so and then uncheck play on awake you only want is to play when we hit it not when we start the game then go back to you is descript and grab the reference to the audio source Oh useless great and then same with before and then the same way as we play the particle you can just play the audio source as well audio source not play so now when we hit the box we should play the particle we should play the sound you should disable the right then we should disable the Box Collider and then we should wait until the sprite particle system has played its particle and then we destroy the game object that's right perfect it works exactly as we wanted now I hope you learned something new and I hope you enjoyed it thank you and bye bye
Info
Channel: FreakyFeet
Views: 17,298
Rating: undefined out of 5
Keywords: aidin, aidin dadashzadeh, freaky feet, freakyfeet, 2d, platform, platforming, game, game development, gamedev, gamedevelopment, how to, indie, indie development, indiedev, indiedevelopment, learn how, tutorial, unity, unity tutorial, unity3d, video, video game, youtube
Id: jZYxls-TOZ8
Channel Id: undefined
Length: 15min 14sec (914 seconds)
Published: Tue Aug 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.