In this part of creating a tower defense game what I'm going to do is have the towers shoot for themselves
so they're going to become autonomous.
Now that means I'll need to turn and target the grooves.
It means they also need to pick a group out and there's an awful lot of them traveling across your little
game surface.
So to pick one out what I'll do is put a kind of border or a target area around the tower itself which
will define the range in which the tower can pick objects up so that when they go out of range of the
tower it will just ignore them.
We need to define that that space around the tail so grab hold of the Taua and drag and drop it onto
its Mocca so that we can see it in the saying don't move it.
Remember you want to leave it at zero or zero zero.
Now once you've got it in there over in the inspector to that prefab we're going to add a colada.
Now you can add any claw at all I'll create a sphere colada around it and that will define the size
or the targeting area.
Initially it's going to be quite small so we want to kind of change the radius and just blow it up a
bit so that when the grooves are like this within this distance it will then shoot at them.
So let's make it where we want to we want to test is working so we don't want it to be.
That encompasses everything.
So mine is at four point six.
Let's make that four.
Now the code that we write for this detection of something going inside of this collider.
We want it to be a triggered code.
We don't want this collider to represent a solid object so that nothing can pass through it because
we do want the goobers to walk through there.
So on your Inspectah for years the clock tick tick the trigger box and that will allow us to then run
an on trigger enter on the script that is on that gun.
So let's now apply that back to operate and remove that from the hierarchy.
Now let's go into shoot code where we will modify it to work with that new area that we've just added.
First of all we'll need a game object and that will be to store the goop that wanders into that space.
Next we need to detect when the guru has walked into that Kolata so that we can then add it into this
group object here.
So just underneath here I'll add in an on trigger and tell which Whyte's for the collider to be hit
by something and that comes through here.
Now if that something has a tag of Gubar on us the goop prefab is already tagged with Cuba.
And just to check that if you switch back quickly to Unity find goove and then have a look over in the
inspector you'll see that the tag says GUBA.
Now back into the chute if we have hit a groove and we're not already got hold of a good.
So as long as this group up here is null or empty then the gun doesn't already know about this.
We want the gun to grab hold on just one of the enemies and keep shooting at that enemy.
Now then we're going to if that's the case we're going to sit the object that's being collided with
intergroup then were going to call invoker painting for a shoot bullet method which doesn't have a space
in this shoot bullet spelling the same as your method you credit before and it will start immediately
and go one second after that.
Now with this mechanism for studying the shooting that means you don't need what's going on inside a
star or we can just get rid of that and all just commented out for now.
Now instead of just instantiating the bullet when we shoot the bullet we also want to play a sound which
will attach in a moment.
So they still get component audio source plie will run the sound that we attach to our gun object then
we will test if the goose is dead.
If the guy is dead.
So the bullet has hit it.
Then we said that good hold up to now and then we cancel in vogue as shooting or shoot bullet I should
say just there and that will stop that from shooting over and over again.
Now you can say here that we've got the squiggly red line of death under our dead parameter here.
That means that it's not public.
When we wrote it before it moves caused some to say this card and flip back to move and where we've
got booed dead equals false Let's make that public like that and we'll save that as well.
I can't get it.
Go back to the shoot that we'll get rid of the era for there and we're pretty much ready now to test
this so that the first group that walks inside of that collider will get targeted at the only thing
is currently the gun will start shooting but it will just shoot in the direction it is facing and we
probably want it to turn and look at whatever it is shooting at.
So down in the update what we're going to do is run a look at.
So this transform dot look at and we want to look at Goob don't transform that position now we can't
do this all the time because he might not have hold of a good object so let's go.
If he does not no then we will look at it like that.
OK so I saved that and will now go back and Dooney and try this out.
First of all we want to put the sound onto our tower so if you just click on your tower prefab there's
a Canon tower sound that's in that same fold if you drag and drop that on to the inspector.
It will automatically add an audio source for it.
Now it will by default be ply on a white.
Just turn that off.
You only want it to apply when the code is telling it to.
All right so now we're ready to try this out.
Sir let's press play.
OK.
This town was put down right on the edge so that we can get a bit of distance between the games.
So let's bring them in put them at the back here so that they're a fair distance and outside of the
Kolata.
Now I'll turn them so that they come in towards a stop in this way.
Something you'll also notice the size of the gun has now turned around and is shooting at the gate.
Is that when I turn this car.
Remember before we turned the car everybody moved relative to the car.
Now however they're not.
Whichever direction I send them off they keep going in that direction.
But take a look at what happens with the actual gun so the gun was the first Mocca that we fan.
What happens if I turn it around.
It has become our reference point or I guess like has zero transform for way all of these goobers and
other objects in the world because it was the first Mokka.
Now if I just pause these and we have a look at our mark so we've got to crepe Taua and we have our
tower Tower and Actel card underneath the ash the tower card that was found is at 0 0 0 when we were
looking for the woods and we were looking like.
Remember the one in the middle.
I was at zero and then the ones on either side had one or minus one in the exposition.
The creep if we look at that and click on the COD the COD has a transform that's not 0 0 0 for its position.
The first card that is found becomes like your reference or your origin point for the whole augmented
world.
So in this case because we found the tower first everything's going to move relative to each.
And if you move that Mocca it then will affect the entire game board.
So can you think how you might overcome these well what I would do is I would create another MoCCA and
use that Mocca as the origin Mocket that you always show the camera first.
You don't even need to be instantiating any virtual object onto it.
It will just then automatically takeup 0 0.
And all of the other maake because that you show after that will then be positioned relative to it.
And so therefore you end up with this like even playing area for all of the augmented objects and that
will then fix this problem that we've got.
Not that it's I guess a real big problem.
It is.
It is with the gun.
I guess if you haven't started the Gabes first moving and then you adding the gun and you're trying
to turn the gun and all of your characters are moving with it.
Then yes of course it becomes a bit of a problem anyway.
That's not what we're working on right at this moment but we're working on is getting these groups to
walk inside of that light.
And then when they do they get shot at then once a group dies and its object is destroyed the gun will
go and find another go.
Now we want to make it so that the goods could actually walk straight through and that gun could miss
them.
So let's have a look at that case.
Or stop playing.
So a few things that were going on today back in ASH chute.
Let's going to hear first of all we're got to slow down the bullets and how fast they're coming out
the gun so let's let's give it three seconds between reloading and shooting again.
Then we're also going to slow it down just a little bit and make it more accurate by adding in a slit.
So we're going to replace the look at it with a slip instead.
So this is exactly the same card that we've got back in the movie script for having the goop turn around
on a curve except the direction this time is taken from the current good that we've got hold of and
the position of the gun.
Again we need to have a turn spade declared and we could do that up the top.
Just keep it down so we can just keep in on it flow it turns speed equals 1.5 F and you can change that
as you need to make things faster or slower as far as the turns goes.
Now you also want the gun to lose interest in its current Goob.
If the goop goes outside of that collider range so that we can add in an an on trigo exit is put just
down under here which will on trigger exit if the object exiting is the goop that we picked up before.
Not something with a tag good because that could be a lot of things it's one that were aimed at.
We will then forget about it.
So we'll just sit and holder too.
No.
And then we will cancel invoking of the shooting so now we should have enough time and possibly an accuracy
without turning that we've just added in so that the groups can safely make it across that sort of collision
radius.
So save that.
Now let's go back into unity and we'll play it again.
So first of all let's again find out how to put it down there and then we will get our games and have
them walking.
Bruce
so you can see that the gun now has quite a nice slip and it follows and it keeps following even when
it can't shoot which is nice as well.
Now I'd like you to do a few things to finish your project off a little bit.
First of all I've added in an arena card that you can see there and that will be the first card that
you put down which will kind of give us all zeroed.
Ground level for all of the other Mocca so that when you move those.
Everything doesn't move relative to it that arena card is attached as a resource to this lecture so
that you know the arena is active.
I've also added in a flag if the ex-model that you can put on to that.
So add that also create yourself some more Talla cause you see got three of them here that are all working.
The Tele has a transparent sphere.
It shows you its range so that speed is matched to the size of the Kolata that we added recently and
we still have the single Taua as well.
So you can move those towels around you can move on start any sort of effect where that lying ground
plane hits.
So post the video now have a go at adding those four different things by yourself.
And then when we come back I'll show you how I put it all together.
Before we start I'll just address a era that I keep getting that's running in my bulat move code that
you're probably getting as well.
If we open that bullet move up you'll see that I've used destroy immediate.
Now the game isn't locking that much if we get rid of the immediate after the destroy then that should
just help us out and get rid of it because Unity doesn't really Lahiya using destroy immediate unless
it's absolutely necessary.
And if your object such as this.
In this case is still interacting with another object and you destroy it while it's doing whatever it
needs to do then it's going to cause errors so let's just change that to destroy instead.
OK let's go back to unity and have a look at the rest of this project.
So first of all you'll need to bring in your arena card which is attached to this lecture as a resource
and also the flagpole which is an FBI X file.
The arena card is a J peg fall.
Now in the WTC editor you can go into there and add it to your W.T. cards.
You can see I've got Monet if this editor isn't quite working correctly for you then you can do that
also through the wiki studio Web site.
So that's the card added once it's added.
You can then make it another trackable object.
So all I did over in the hierarchy under my Emmie's tracker was copy the tower.
Duplicate it.
Call it a rainbow.
Then I set the active card to the one I just added because it will automatically pop up for us.
Now the flagpole itself I didn't just add the flag pole so I went through the process that we've done
before is pre-view that matka then drag and drop the mesh that we would have got so this flag pole IAA
onto the arena.
Now you can see that doesn't show up because it's transform is a little bit strange so I scented this
on 0 0 0 which brought it up there and then I had to scale it down to zero point 1 0.1 and zero or point
1 It only needs to be big enough so that you can see it when it's in the augmented environment that
flagpole then dragged and dropped into my project folder so that we could create a prefab out of it.
So let me delete that then that prefab went into the drawable item on the arena.
So this is my flagpole prefab not the mesh.
So that takes care of the brain that you want to show up first the extra towel costs themselves well
they were quite easy because all I did was just print out two more copies of those slightly smaller
than the original one I had.
Even though you train a mock at a certain size is no reason that you can't actually use a small version
of it.
It will just scale down the model on it because the augmented system will think that it's further away.
And so that's fine.
OK so that's the towers now.
The sphere around the tower themselves if I just grab hold of my prefab Tele which is this one here
dropped on my tower.
And if we have a look at what it's got on us zoom out a little bit you'll see I've added I read Sophia.
All I did was right click 3D object at a sphere.
The sphere that was added You have to remove its Kolata then to that Sophia I added a transparent red
material.
OK so you just create a material then in the rendering mode set it to transparent and then in the color
itself you want to drag the alpha channel down quite low will make it more and more transparent and
then attach that red or drag and drop it onto the sphere.
Now the Speace size needs to be scaled to 8 8 8.
Remember that the radius that we sit at Kalat of Tooheys fall which would mean that the diameter of
the sphere we're creating is going to be eight.
So it will fit quite nicely around that.
Once you've done that you want to apply it and that will give you your Taua and then you're good to
run.
So that's the end of the tower defense project.
And now you can play around with that to your heart's content.
Actually one thing I did forget to tell you is that as you add more and more markers don't forget in
the image tracker that you need to increase the concurrent targets.
In this case.
I've got six wicket you doesn't necessarily like that much but it still works OK.
So happy you enjoyed that.
And it's given you lots of ideas for your own projects.