In this lecture we're at the stage we're ready to add some bullets to our gun Taylor.
Now we're going to need to see the Guntown in order to create these books so we can get the size right.
Well you want to do is grab hold of your towel object and drag and drop it.
This is the prefab from your assets folder onto the tower Mocca.
Make sure that you don't move.
OK you can move yourself around in the sane.
But it's going to remain at zero zero zero.
Otherwise it's going to render in the wrong spot.
Now we want to be able to shoot bullets.
And you want them to appear at the end of the nozzle.
In this case or Target or whatever it's called.
So what we'll do with that Taua is selected in the hierarchy right click and we're going to create a
date or what I like to do so I can see what I'm doing is create a cube.
Now the reason I do that is just for positioning purposes.
Now this cube is going to be where we will spawn bullets.
It will also dictate the direction the bullets are going to go in.
So you want this zid axis of your q or M G game object that you're creating to be facing out a white
from the top.
Now I'm just going to write this a little bit small so I can get it in exactly the right spot.
I mean just the CH
on that I'd be in a little bit.
If you're Taua has a kilometer on it make sure that you instantiate these bullets or at least put this
position for the bullets.
Outside of that Kolata because the bullets are going to have a cold water on them you don't want them
to instantiate inside the turrets Collider and then they would just hit and fall to the ground.
But this particular target itself doesn't have any coladas on it so we don't have that problem.
Now with that cube done either in the hierarchy are just going to call that cube the spoon pause and
then I will delete its Mischer enduro and its Q Just get rid of them.
So just remove those components remove the box clutter as well as very important.
So then you end up with just the equivalent of an empty game object.
And it has to be a child of your prefab.
So now that it is and its in the right spot for us you want to then click on apply to save that back
to all of your tower objects.
So now we just need some bullets of the right size.
Now these bullets are going to be separate game objects so click away from anything in the hierarchy.
Right click 3D object sphere.
OK obviously thats going to be too huge for a bullet and will just resize it down.
And you want to get it to the right size.
So when it comes out of that target its not going to look too odd.
So thats ok.
Yeah I'll stick with that.
OK so the Safiya over in the hierarchy Lets call book and we need to make it into a prefabs so will
drag and drop that into our projects fold up so that we have the bullet we can delete that and the same
in the tower.
Make sure you've applied it very important.
Applaud all the changes to it and then we'll delete that out of the hierarchy.
The bullet obviously has to move.
So let's add some really simple code to the bullet to make it do that.
In the project you want to create a new C-Sharp file.
Let's call it bullet moves and it's pretty similar to the way that you would move the Goob object forward.
So to the interior of the default class that you get which is going to replace it with some simple code.
First of all we've got an on became invisible.
We've used this before with the zombies to make sure that when they went out of the range of the camera
that they were destroyed.
So we're going to be shooting bullets and probably a lot of bullets if they go outside of the camera
and then we want to get rid of them so that the game's not processing them anymore so we just put a
destroy immediate in here for the bullet.
So this game object and in its update all we're doing here is moving the bullet for the bullet itself
has no intelligence whatsoever.
It doesn't know about goove it doesn't know much about the environment it just knows to move forward.
So it just keeps going along.
It's Zayde axis and it's up to the gun itself to change its orientation so that the zid axis of the
bullet itself can change around and then push forward in that other direction.
So with this done just save that bullet move script go to unity and you want to attach that to the bullet
so just drag and drop that code onto the bullet proof FEBE once again select that pre-fab and then just
check in the Inspectah that it was attached.
I always do that because sometimes you think you attach something and it's not actually there.
Now you write some code for the tower.
So once again create another safe shop script.
Let's call this shirt and we will open it up for editing.
Then the shoot script needs to know about the bullet prefabs so that it can instantiate many copies
of it.
So let's create public game object bullet.
It also needs to know about the spawn point.
So put public game object and then spawn pause Okay so with that say it switched back to unity.
Now you want to drag and drop the chute onto your tower case or drag and drop that from there on to
the tower.
Then click on the Talla and you'll see that you get a space for the bullet and a space for the spawn
poles.
So the bullet you can drag and drop from the assets into bullets and then the spawn pause.
If you roll out your tower you will find the spawn pause that we added to it before inside of there.
So just grab that and then put that up there.
Now currently we're not instantiating anything inside of that chute script.
So let's do that now let's go back into shoot and we're going to create a new method for doing that
so void shoot bullet.
And then in here we will do and instantiate all of the bullet at the spawn pause position and the spawn
pause transform rotation.
Like that.
OK so we now need to trigger this shoot bullet so just for testing purposes to begin with it's got to
stop.
And we'll call and invoke We're Paging on that so we will call shoot bullet and let's do it one sick
and in and then every one second after that.
So that should be quite fast.
Let's say it switch back into unity and now we can actually try.
So when the Taua gets instantiated it's going to start running it's code so press play and this time
we want to be on the tower.
And you can see there.
I think that it is instantiating the bullet but the bullet what's happening to the bullet it's going
really really really fast.
So we're way too fast for what we want right now.
So let's slow that down.
OK so stop playing and we will go into a move book Courage and let's change the train's light on the
bullet.
Let's make it zero point one.
If I say it and let's try that out again.
So now we can see the bullets moving through the air and you could slow that down a little bit more
if you wanted.
The point here is that if you have a bullet that moves so fast you can't see it then there really is
no reason for you.
Have a mesh on edge because otherwise it's just processing power that is not used whatsoever.
So this is a Nazi example of being able to see the projectile that's coming from the gun.
Often when it's a really really fast moving projectile you don't actually have a projectile mash in
your game.
All you're doing is just doing a recast and then determining whether you would hit something or not.
Not going to the effort of creating a mesh and pushing it out.
But in this case it looks cool to have that bullet flying through the air and it could change the difficulty
level of course with these two.
You could slow that bullet down like really slow.
Also the why we're moving that bullet it doesn't have any gravity affecting it whatsoever it's just
moving forward and it's going to move forward continually until it gets destroyed on its own became
invisible.
OK so now that's working.
And you can see that as you move the gun around it's going to shoot in different directions.
Now you could do that by hand if that sort of mechanic you want in the game.
And so yeah.
Well why don't we try that for now.
We will try and have a user directed gun to start with.
So in order for us to tell if we've hit out Goob object we're going to have to it a little bit of code
to detect any hits between you and the bullet.
Now remember that Goob has the own trigger enter event going on.
We're testing for where the home is if it's hit the home we could also test if a bullet has entered
into that trigger point.
So let's tag the bullet object select your bullet prefab go over to the inspector and we're going to
add a new tag and let's call it bullet with a lower case.
They save that then make sure that you select the bullet prefab and tag it with that bullet.
Then we go back into grooves code which is the move.
I pick that up and we're looking for the on Trigger into code.
So just in here above the harm we're going to add another statement if the game object to collide a
game object tag equals harmless just copy all of that and we'll put that up here.
And then instead of home we're looking for bullet.
So put a bullet in there.
And then we can change this to an else and then if we hit by a bullet we're going to run a method called
heat like that.
And then we can add in the hit method under here and that's going to be really simple.
So all it's doing at the moment is destroying the hit game object which is the one that has this piece
of script attached to it.
In point one of the second once the bullet has hit it.
So let's just save that and go back into unity and try it out.
So first of all we'll get hold of a good character and have him walking across the screen just maybe
they will get a Taua and just say the town is now shooting the balls off.
Never put it on the same plane as the goves and shoot over in that direction.
You'll say that the bull's eye missing then actually going over their heads.
If I just slow it down a little bit you'll see that I'm starting to hit them and will actually be wiping
the ash.
So you might want to consider making the gun tower a little bit shorter if you want to be able to automatically
hit them on that same height.
All you can about the grooves a little bit larger as well.
All right so that's working nicely.
Let's go back and add a little bit of animation to the game.
So it gives us a little bit more of a dramatic drawing sequence just before it hit the car.
I'm going to select my tower from the project and then just change it skyll and bring it down a little
bit to point four point four and then point four and we'll see how that goes.
Later on when we're testing the shooting of it whether it's now down low enough so that we don't have
to tip it anymore.
Right so now we're going to go back into our move code and add in some things to help with the animations.
First of all we need to go up to the top and we're going to create a bhool value called dead and we'll
set it to false because initially Goob is not going to be dead.
Next we're going to go down into the hit if it's hit then we'll say it did to a cool tree.
So that group is dead.
And then lastly we're going to use these dead down inside of the update before we do anything else.
We're going to check if dead is true then return.
Now what this is going to do for us is to flag first of all when group has become dead group is about
to play an animation sequence of drawing when it's drawing.
You don't want it to be turning towards the tower and moving at the same time you want to stay in the
same place that it is.
While that death animation plays out.
And then once it does you're going to destroy the object.
So back up in here it will add a few lines of code to help us do that.
So we have an animation on google that we can get.
And it's called Annam dot says trigger which will trigger it.
And it's is dying with a capital D and a capital I.
Now of course if you want to use the animator attached to your character do you remember what you need
to do with it.
Well you need to declare at the top and then initialize it in stops.
So up here we're going to go animator and them and then down in the stack we will go and equals this
don't get component animator.
Now the other thing that you want to do also when you kill a character it's playing its death sequence
besides have it continue moving along the ground is you want to make it not collectible by anything
else.
So before we destroy the game object itself which we will now do after four seconds which gives it enough
time to play the dying sequence we will destroy
its Kolata and rigid bodies so this don't get component
kuliah in one second and then we will destroy the start get component
rigid body in one second.
OK.
So now we're going to play the dying sequence which is about four seconds long good is going to stay
remaining alive for that time to play that out and then we'll get rid of it while that's happening we're
turning off any collage in that so they don't get in the road and cause physics problems with other
things that are in the same.
Right so now let's try this out.
Save it.
Let's go back into unity and let's press play.
First of all we need to find an object and then we'll grab a Taua and let's now a bit funny.
OK.
See if we can get them because those balls look like they're still going over the top for May saw just
angle them down a little bit and well.
Now you can say that they're dying and playing the death and a motion and falling over
and it looks as though it can actually hit more than one at once to with that bullet.
So the bullet doesn't get destroyed when it hits a good it's getting destroyed when it goes outside
of the range of the camera so it's just going to keep going and probably get rid of a whole heap of
them if I stand behind them and just what the mouth from this direction.
OK so that's working really nicely.
Now if we introduce the Hayyim then we're going to get them going towards the home and they will disappear
themselves to see if we can get that in.
So now they're going to try to go higher.
And while they're doing that we can try and stop them
can we should be a really really good shot from behind.
It a fair few of them that.
OK.
So that works really well.
Probably don't need to make that tire just a little bit shorter again so that it isn't hitting the top
but in the next video what we're going to do is make that target auto targeting so that we don't have
to touch it ourselves.
We just have to position it and then it will just move its tire so that he's aiming at the closest Goob
object to it.