Enemies / Timing the Attack
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 there's a couple of more things left to do to make this look really good. Right now the turret is shooting around uncontrollably and would probably destroy everything in its path just because you're in close proximity to it. So we only want it to shoot when it's rotated enough to actually see the player. Now of course we could do this by doing difficult calculations with angles and stuff like that but I think I found a shortcut that you'll like. So let's open up our turret script and let's go down to where the invoke repeating is and as you can see the invoke repeating function that triggers the animation of the Falcon is set from the root of the script. So it's invoked right away as soon as the turret is created in the memory of the computer. So we don't need that, what we need to have is different states that the turret is in as in attack or idle. So to do that let's create 2 more functions, one of them is called attack and the other one is called disengage. Of course the invoke repeating function will be triggered from the attack function. Alright, now to make this work we need to add some variables up top here and, for example, the variable mode which will be by default and idle. So when our turret is idling it's not looking at destroying things. OK, also what we need, let me copy some text from our effects here, is turn the effects off by default. It only needs to be on when it's actually shooting and of course the best way to do it is just add that into our Awake function. Now that we have that covered we need to switch the modes into out functions and check if we are currently attacking or not. So in the attack function let's type if the mode does not equal attack so we're not on attack mode so we're in idle more or whatever else you might make up in the future. Right now we need to go to attack mode. Next, we need to do the same thing in the disengage function, if mode is not equal idle and then we need to add some code there. Now, let's start with the attack function, it is invoking the repeating and the repeating will turn on the animation of the flames and then it set the mode right then to attack so that's fine. Now for the disengage function it checks whether our mode is idle or not and apparently right now it is not. So here we can cancel the invoke, I'll type cancel invoke and next what we need to do of course is set the mode back to idle. But that's not all we also have to turn the flames back off. So that should work just fine, except that nothing is right now triggering the attack function. So what we need to do is trigger the attack function from our scripts up here so let me copy this and it only happens when our player is detected. So let me replace the print function with our attack function and add a semicolon and then copy our disengage function and copy that whenever we are not supposed to be attacking so before every return false. That is when are out of range or when something is colliding. OK that should do it but now for the part where as soon as we start attacking it will still invoke the repeating and the flames will shot uncontrollably. Now it only needs to do that a couple of seconds into it when there's been enough time to rotate. So let me just change this number to 2 seconds and the invoke repeat will only start two seconds into it. Now let me save that and Unity is telling me I have some error somewhere and it says, unknown identifier attack. Let's see what that happens. That of course should be a string in between double quotes. Alright, save that, the error is gone. Now let's see what our script is doing. Our turret rotates and then two seconds into it starts shooting, very cool, very nice, exactly what we want. Now I'm hiding behind the cube and it stops shooting and its' not looking for me anymore. Now if I come around from the back, it starts finding me again and after two seconds it works. Now if I'm our of range like right now it will not find me until I get closer and then two seconds from there it starts shooting. Very nice, we fixed that problem pretty swiftly and one more thing, let's change the mode variable to a static variable so that we can access if from other scripts and this way we can see whether turrets are attacking or not. Perfect, 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 