Well come back to this second part of the tower defense game that we're putting together.
So how did you go with getting the sport to working on your creep tracker.
So what I've done in order to achieve this is first of all I created an empty object from which I made
a prefab So this is the prefab.
Here let me bring it back up so it's an it was an empty object a cold spoon to and then to it I attached
a sport to script and that spawned a script which I'll show you in just a moment has Goob passed through.
So this prefab in the assets fold up passed through here as a game object so that the spawn can then
instantiate it.
Now the sport is also positioned at 0 0 0 so that when it appears on the crape.
So if we click on the crepe trackable you'll see that the spawn is sit here as the durable item.
So this Borna the code that's it.
Let me open that up and it is really really simple.
So it has a public game object of Goob which we pass through.
And then it has to invoke repasting starts immediately.
And I've got them at point seven apart.
So you might have that different interval.
The Spawn method that's getting invoked is just creating a goob game object from the prefab is being
passed through at the transformed position of the sport.
So the sport is at 0 0 0 therefore it will sit right in the center of the matka and that's where we
want Goob to spawn so that's fine.
And then the rotation is also going to be the same rotation as Grubin then gives going to move off in
a zid direction based on the code that we've got on that object.
So very very simple so attach that code to your sport to make a pre-fab out of it.
So let Joe create drag and drop that onto there and then make sure you select your sport to the enemy
assets and drag and drop Goob on today.
Then what does this look like.
Let's press play and have a look.
So we find our mark is there and we get good spawning.
Now what we also want in this case is we want goop to go towards our home so wherever we have that start
here.
Mocca we want the tubes to go off.
Out from the initial starting direction just like they are now.
But to curve around towards him and then when they reach home we want them to make a little noise like
they've survived the journey and then make them disappear from the game environment.
So let's now start working on the code for that.
Now in order for goove to turn towards that home tower it needs to know where that Hoehn tower is.
So it has to be able to find its position and find an actual game object in the hierarchy so to do that
we need to tag the tower grab hold of the home pre-fab drag and drop a copy of it into your saying but
put it as a child to your home trackable MoCCA and then you can zoom in on it.
The reason I've done this is so that you don't accidentally change its location because if you change
its location then when it appears on the Mokka it's going to be in the wrong spot.
And we just don't want to play around that.
All we want to do here is add a tag set up in the Inspectah we want to drop this list down add tag and
then you'll see that there's already a good bit tag in there because if we click on GUBA we'll find
that that pre-fab is tagged as good but we'll use it momentarily.
First of all go back to your home and we will add a tag and let's call it home.
Save that.
Click again on your home and set the tag to home for that object.
We're going to also determine when a group has reached the home with a collision event so that when
the Google object passes through the home then we will consider that home and destroy the object.
So in order for that to work home has to have a claw on it.
So select again your home prefab add component.
And we want to Kolata that's around.
So it doesn't really matter what the shape is I want to add a box colada.
Now you'll see it just down here maybe briefly it needs to be re saw it just in the why and you'll need
to lift the center up as well.
So just you know get it.
You just want a big enough so that when good does walk through it then it will trigger the event to
occur.
And that's what it's doing that's the only reason it's there.
Now the collider has to be on the object that has the tag on it because these two things will work in
unison inside of the movie script of Goob.
Once you're happy with that click on apply to Cybil those settings back to the prefab.
And then just delete that home pre-fab out of your hierarchy.
So to turn good let's go into the move code first thing we will need is to know where that home object
is so let's create a public game object harm in fact you know it doesn't even need to be public because
we're not using it outside.
We're going to get it in the Stosh instead.
So let's go home.
Cool's game object.
Don't find with tag.
And we're looking for something tagged with home like that.
Now before we push Google forward using this translite we will just turn the model a little bit towards
the home.
So first of all let's just do it really quickly and snap the model towards home then you can do that
lot.
So they start transform don't look at.
And we would want to look at the position of home which would be home transform dot position.
Now this is the quick and dirty way of making something turn around and orientate.
It's zid access at something else's position.
So save that and we will just go back into unity and have a look how this works.
OK so let's press play and we'll present our card to the camera so it can find.
Right now.
So we've got a whole bunch of goves appearing on there now.
They're not moving forward anymore.
They let me grab this home to present that to the camera picks it up.
And you'll see now that the grooves start walking towards it as I turn this marker around.
First thing you'll notice is that every group moves with it because they're in the same transform space
as that card is
any of the movies that were created after the Talla appeared moving towards it because I know about
the ones that aren't stuck on start the picking up of the home object is inside of their stop function.
So when the stop function runs if that tower doesn't exist then they don't know about it and they never
ever get to know about it.
So if the home tower disappears then you also end up with some issues so that when it comes back again
there's problems if it disappears and is not in the scene anymore.
Then there's also going to be problems and you'll get like no references and all that sort of thing
going on.
And I've even got errors down the back of my code saying no reference exception which will be from those
initial groups that could not find the harm.
So we need to just make a little tweak in the code to fix set up.
So let's go back in the code here.
One way to pick up home constantly would be to move this line down into update which we all do which
is not ideal because finding something with tag in your hierarchy is really quite labor intensive.
So instead you could maybe have an invoke that now and then checks whether it's found in the home or
not and stop.
Otherwise what you could do is put something around here saying if Haim does not equal no then turn
towards it otherwise keep moving forward.
So let's say that and we will just try it out.
So what do we get this time.
Whoops.
OK so we get goop and grooves.
Now they all wandering off.
They don't know about home and will bring the home back up put it down.
Notice that these guys over here are now walking off.
They never ever knew that home was there or that home ended up appearing.
So they're not constantly checking the ones that have reached the chow all sort of fit around it's Centrepoint
we're not destroying them yet but they're constantly looking towards the center of the tower.
And therefore you end up with that sort of really odd behavior.
And if I turn this cod besides the fact that it does turn the light that transforms them according to
the cod it then allows them to snap Bacharan and move towards the tower which Yeah it doesn't look very
pretty.
So let's just make a few adjustments.
The code to fix that up.
So back in our code the first thing I'll do is just make this picking up of the home a little bit more
elegant.
I'm going to create another method let's call it for wayed find home and it's going to do is try and
find time.
So we'll take that law out of start put it in here.
Then in stock we will do an invoke repeating and that invoke repeating will run find harm at time 0
and then one second after that in a complete loop on till we tell it to stop which we will do.
Dan in here.
So we will say if home does not equal null after we found it then we can actually cancel invoke.
So what that's going to do is that good will start and then it will start to run this find home if that
home object is in this scene then it's going to find it immediately and cancel this invoke if the object
is not in the same.
Then one second later it will run out again if it's in the same.
It will stop it if it's not.
It will run it again.
And so will keep doing that for you.
So these turn when we run it we should get the ability that any games that went off on their own to
begin with going to find that tear and then head towards it.
So let's find an k then I go.
So they're off on their own.
Now I'm going to grab the home and bring that into the scene.
And you can see that these guys over here who went off on their own suddenly knew about the home and
then they're heading towards it.
So that's why you haven't lost any of the graves forever.
They're going to come back at some point towards the home object.
OK.
So that's good.
Now we're going to make that movement a little bit smoother back into the code at the top.
Will will need I know the space.
So let's create a float turn spade we'll call it.
So this is how fast that good will turn around on the spot.
And we're going to make that equal 1.0 or F like that and we will use that down inside of the update
to do a slurp rather than a lookup.
So a slip is a really nice way of moving a game object by slowly turning it a little bit towards its
target.
Each update loop.
First of all we need define the direction from Gibbs position to the home position.
This is a straight line between the both of those objects then we perform I slip now a slip gives you
back eye rotation amount between two vectors.
So first of all we take the vector of the way that GUBA is facing and we then type the vector towards
that direction or I should say the angle towards that direction.
And then we turn a little bit through it using turns spade rather than turning all the way through that
angle which is what look at does we just do a little bit at a time.
Right.
So now we only want to turn if we have harm of course so let's say that.
And you should see a dramatic change in the behavior of their movement and it's just so much more natural.
OK so let's grab this and let's have them going off in that direction.
The other thing is you've got this direction all set they want to snap around and find home.
So we'll grab at Taua.
Now if you watch them see how they smoothly turning around they're going off in this direction but this
smoothly turning around.
Now if I get this stock card and I turn them all around like that.
Now I know the whole lot of them moving according to their transform.
You can see that they start off still on that beginning Victor that they've been set to and then head
off towards here and now the hole seems to have a bit of a plug situation going on because we're currently
not destroying them when they get home they kind of like stuck circling the goal position.
So the next thing we need to work on is making them get destroyed when they reach home.
Now we can do that very simply inside of our code I'm going to do that on the night the start and add
in an on Targa into the on target enta is going to execute when the collider in question is the home
Kolata.
So this is in the move code which is attached to goove So when groups collide the collides with the
homes collide.
It's going to destroy goop in one second.
Not immediately because sometimes it could be some other code that will complain about being destroyed.
I always get a little bit of time to be destroyed one second might be too long for you can make it a
bit shorter.
Then after that we're applying an audio source the audio source is the Ding-Dong wive and it's already
attached to the pre-fab of good that I gave you and you can change that to whatever you like but this
is just going to pick up the audio source on this object and then play it for us.
So very simple now.
In order for an on tree get into Tehran it means that your objects collide needs to be a trigger.
Now that I already gave you has that so save this card go back into here let's just click on the Goob
object come over to the inspector and just check that it is a trigger.
See this so captial Kolata is a trigo.
So we will use that and your code will work.
All right so now let's have a play.
So first of all this fund Goops and we'll put them down there then we'll grab our home and pull lot
up.
Now they'll start turning and walking towards it.
Put it down there and they'll start being destroyed and playing the ding sound and you can hear that.
So that ding sound is actually another reason why you don't destroy them immediately because the sound
for the ding is attached to the Goob game object.
If you destroyed it immediately then the sound wouldn't play because the sand wouldn't exist in the
hierarchy anymore.
So that's not annoying at all is it.
If you get really sick of that do you change it or just Donyell volume down.
We will add some other sound effects when we start to put the tower that's shooting into the game environment.
So that is turning around your characters with the North Slope and setting up a home location for them
to go to.