Weapons / Scripting the Explosion
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now last but not the least, let's use the explosion to blow up our grenades. And usually in game design it's the same- that all the hard work is done and all the way in the end you get the gratification that it finally works. So, what we haven't done yet is save our explosion in to a Prefab. So let's go to Assets and then create a Prefab. Let's drag this into our Prefab folder, rename it and call it -exp- just short for explosion and drag our explosion in to it. Alright- That will do. Now, we can delete the explosion in our world because we've saved it as a prefab. And now we need to add some script that actually make our grenade work. So in the Scripts folder, let's create a new script and I suppose you know how to do that by now. Let's rename that GrenadeScript. And we'll attach this to our grenade. So let's open up this prefab, then drag the grenade on top of it until you see the -plus- sign and then let go and it adds it right here. Of course, it's still empty. We'll add some script right now. Here's what we're gonna do first. Let's define a variable -creationTime equals Time time.- Now, what we'll do is we'll keep track of the time when our grenade is created, that and the couple of seconds after that we can actually explode the grenade. So that's why I'm using a variable called -creationTime.- Now in Unity, to keep track of time, we use time class and then the sub variable time with a small capital -t- and the class of course, is with a capital -T.- So that's how to keep track of time. And now., we need to set or reset this variable as soon as the grenade has been created. Now, one of the pre-defined functions in Unity that we can use to do that is the Awake function. Now, the Awake function is triggered as soon as the object is created. So inside the Awake function, we'll just use -creationTime- once again and set it to it's current value, alright- Now, in our Update function, which again is called every time a frame is rendered, we can check if Time is past since the creation. So, -if Time. time is bigger than our creationTime- and of course we need to add a couple of seconds that our grenade has been floating around in 3D. So for example, -3- seconds and let's use brackets around these to make sure that the -if- statement really checks this edition versus our current time. OK- So if the grenade has been floating around for 3 seconds, we'll destroy this game object. We can simply type -gameObject- then it will refer to the game object we're attached to and that is of course, the grenade prefab. And then let's instantiate our explosion prefab, which we have not defined yet. So, let's go up here and type -var explosionPrefab- and this is the prefab that we just made. And this is of the type transform. So again, to instantiate function needs 3 variables. First, the prefab that will create. Second the position and third, the angles or rotation. So let's first type -explosionPrefeb-, then our -transform. position- and then our -rotation- which is our -Quaternion identity.- Alright- This should work just fine. Of course, if you want to change into a variable -lifetime-, for example -equals 3-, you can do that. Instead of just 3 lifetime. So this way, you can tweak it inside of the editor, if you want grenades to last just a little longer or just a little shorter. Let's save this script. It's already attached to our prefab. However, we have not yet set the explosion prefeb so let's drag the X on top of the explosion prefeb and save this. And now, in order to test our script of course, we first need to find a box of ammo. Oh, hold on. There's one. So, let's grab this and then throw this against the wall. I'll throw it a little bit over. As you can see the grenade go right there. Let me throw it a little bit lower, here it goes. And boom. And as I said, the gratification is always in the end when our grenade actually blows up. Now of course, we should add some sound effects and stuff of the grenade tinkling around when it's still there. And of course, the explosion sound because it's a little funky right now. But we'll get to sound a little later. I can't wait to blow this [unk]honestly. So, let's move on.
Tutorial Information
| Course: | Introduction to Game Development Using Unity 3D |
| Author: | Efraim Meulenberg |
| SKU: | 34092 |
| ISBN: | 1-936334-03-8 |
| Release Date: | 2010-02-23 |
| Duration: | 5.5 hrs / 75 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 