Welcome back to this project on creating instant tracking and a bowling zombie game.
We're now at the point where we should probably add in some zombies.
So what you're going to need for this is to bring in the jeel zombie unity package that's attached as
a resource to this like.
I've already added in Jill here.
Now the other thing I've done that I should tell you about is the SPia we had it like really really
small so that you could see it rolling across the screen and it wasn't taking up all of the screen.
But now this feature has to hit you all zombies and create a collision with it.
And if it's too small it can just pass underneath all wrought by the zombies Kallaugher and that's not
going to work.
So what I've done with this SPia is in the inspector I've updated it's scale to point five point five
point five.
It's probably even a bit bigger that size but you can adjust it as you see fit.
As we move along.
Right so to get started we need to go back into the code for control and what we're going to do is pass
through the zombie prefabs So up here underneath Now I.
Obs prefab which is where a sphere is sitting.
Let's add in a public game of ject and we will call these zombie prefab like that.
So that will always save it.
Switch back to unity and select the control.
You'll see the zombie prefab is now available in the inspector.
Drag and drop the Jill prefab onto that Zombi prefab opening.
And this is going to spawn our zombie forest.
The Zombie has all of the animations on it already so we're just going to utilize those animations and
control them with some script.
OK.
Now let's get about spawning it back in control.
We're going to add a spawn method.
Now let's do this just underneath the start because we'll be utilizing the start to invoke it.
And where is it here it.
He's my start method.
And underneath here I will put in this.
So it's spawn zombie.
And what it's doing is working out a vector which will be our position.
Zed pause using camera don't mind transform doc forwards.
We're using the forward vector of the camera.
Multiplying by 10 which will create is a vector that's going out from the Kimmer for a distance of 10
and you can increase this number if you want the zombies further away or decrease it if you want them
closer to you.
You could also make this random if you want them to spawn at random positions or distances from you
straight afterwards we're going to grab hold of the Zed coordinate all that vict and set it to zero.
So that will move the zombie down because this vector we're actually going to use as a position of where
the zombie will spawn.
And by setting it to zero the zombie is going to spawn on that ground plane.
If you want to spawn anything at a different location up in the air a little bit or under the ground
or wherever you want it then you would modify it.
But 0 is that ground plane where we have a shadow plane and where the grid appears then after that we're
going to instantiate El Zombi pre-fab at zero pause using a basic default identity rotation for it.
Now this spawn zombie is going to be invoked.
Replated so oppin start let's put invoke repasting and we're going to invoke repeat spawn zombie and
let's do it after one second.
And for every second after that again you could make these random possibly the last one because it's
the one that keeps spawning.
In fact you can't put a random number into invoke repasting because it's only going to run once and
then if it gives you a random like 0.5 it will keep spawning.
This method of running this method at 0.5 intervals if you do want to be random then the way you do
is if you're interested in a model will show you will I mean here is that in here we invoke instead
of invoke repeating we give it the spoon lombe name of the method and then we start after say one second
stop stop.
Now this will just run at once after 1 second.
But what you can do if you want to keep running random intervals is put this in vogue at the end of
the method itself and it will just keep running itself in heah.
We put a random range and then you would put it between say 1 and 5 seconds and that's going to cause
this to run randomly.
So that's just a bit of a side law and I'll leave that code in there in case you want to see it.
I'm not going to use it I just want the one second intervals apart so that they're nice and even.
So now we can test this let's just say go back to unity and we're going to press play.
I'm going to point my camera is always at the ground start the tracking.
Now you can see that I'm already getting these prefabs started because it starts immediately and you
could set them to stop that invoker painting with one of the initializations or one of the seen fans
or whatever.
Now we just look up there I've already lost my tracking and that doesn't matter at this point because
all we're doing is warning the zombies and you can say here that we've spawned the zombies they're being
spawned.
Straight ahead from the camera at a y position of Zarar which will be Rosh monay along this orange line.
Now if we move the camera us look over.
Maybe Otherwise a little bit.
So let me just see if I can.
And this just a little bit.
There we go.
And now so I'm saying my very attractive garbage bin in the corner of the room you can see that the
zombies are all so starting at a different location which is still directly ahead of the way that the
camera is facing and they walking across.
And here is my very large Sophies.
Let me see if I can find these in the scene view.
He also HIZO the zombies being spawned and the speed is just pass straight through them because there's
nothing there to tell them to die or to do anything.
And off they walk into the distance constantly spawning never being destroyed.
All right so that it's working.
Now we want when we move the camera.
So if we move this camera up and we lose the scene like that we've lost the speed because they were
already programmed to disappear.
But see how the zombies are still there.
They're sort of stuck being shined because we're not turning off their render out when we lose it.
So it just stopped flying.
Now we'll go back into the control and add the code for getting rid of the renderers.
Now we will put that where we get rid of this feeds which is here.
So in sit scene enabled we've got this code which gets rid of the Rangers on the seas turns them on
or turns them off.
We're going to grab all of that again underneath here and paste it in just remove the re declaration
of that so he doesn't give us a problem.
And instead of looking for space this time we're looking for something with a tag of zombi.
And if you go look at your jeel zombie prefab you'll see that it comes tagged with zombie.
And if you do find you don't have that tag there you'll have to add it so that we can find all of those
zombies so let's just say that I'm going to switch back into unity and I'll just click on the geo prefab
to show you where that tag is.
So just up here to make sure that you tag it as Owmby.
Time for another test.
We'll press play.
We will down and start tracking and then move this up slowly two to either the zombies
can see that the spawning in the distance again this is just going to look so much better when we build
it out to the device because it doesn't lose the cloud data as quickly as this unity one does.
Now you can see them and I can add some Sophies into the scene and then if I go off and lose it you
can see that they've disappeared.
The ones being born however you'll notice haven't disappeared.
Keisel it was the only ones that existed.
Now the reason for that is that the sporting obviously Cape's sporting even though the scene has been
lost and the zombies are only turned off and found at the time that the scene is lost.
Not constantly.
So this actually does force us to move where that invoke is how that invoke is happening.
And being able to control it.
So let's stop playing and go back into the code.
All right so we'll put it in the set saying enabled.
So where is method down in the start.
Let's go down here.
So I'm going to take this out of stot and I'm going to put it up in the sit scene enabled so after the
greed Let's go.
If I build
We're going to invoke repasting.
Otherwise you and I will do is false and we're no longer wanting to have zombies spawning.
Then we can cancel invoke and cancel this born zombie and that's going to stop us getting news zombies
being spawned when there's no real scene to spawn them into and then they won't be in the distance.
OK so save that and let's go back to you know we run it to test it again.
So pressed play and before doing anything just taking notice of what is happening down in the scene
they are still spawning.
Now that's fine.
It's not because we're trying to stop that but just have a look.
This will stop the tracking.
Then we will move this up a little bit.
So there they come they're still spawning.
If I lose this they disappear.
And they stop spawning which is great which is what we wanted.
So the only issue here is that this morning right at the beginning because that invoke is actually being
invoked to run because that scene and I will is being set to true right when everything starts up.
So to get over that let's go back into the control just put a sneaky little bhool is spawning at the
top here.
Equals false initially and then we won't let it be true until we click on that button for the first
time.
So that button is change.
Now let's put is spawning he calls for they.
And then we can test if that button has been pressed down in Al Zombi spooning method.
If it's spawning
in fact if is not is spawning then we will return.
Otherwise we will start spawning.
And because that gets set to true after the first button press then we're fine to case because the other
stuff keeps turning it off.
Save it.
Let's go back and we will now try this out.
So I pressed play.
Let's have a look.
No spawning happening.
Start tracking now a spawning.
And now we can lift this up slowly to see if we can see them there they are.
And then if we go too far and lose it.
And down again they come back and exploring and so they are stopping spawning when they disappear.
Now if we run this again I just want to point out the next thing that we're going to fix about this
once the zombies start to spawn then we will find that they just head off in a straight line and they
don't hit off with any purpose towards any particular goal because they're not programmed to do that.
And if I lift up the camera I'm going to say that they are coming in that general direction towards
us but not really going off to the side so we can get them to spawn at different spots based on where
the camera is facing.
So if I turn the camera just a little bit like that way and zoom out here and watch where they're starting
to spawn Let's go back in these direction and you'll see this Bourne point change which is fine but
they still head off in the same direction.
Now that is dictated completely by the animation that's on there.
It is no code as such.
Pushing them in that direction.
In fact what we want them to do is to come towards the camera because they're not really a threat if
they're just wandering by and you have no compulsion to sort of shoot balls at them.
So what we're going to do is just stop playing.
In this case and add some new script that will dictate their behavior.
Right.
So in your assets we're going to create another safe shot folder called Chase and open that up.
First of all we will need at the top a public variable or it doesn't need to be public.
Depends whether you want to change it in the inspector or not.
I'm going to call it.
They are.
Stations speed and or set that to 1 this is how fast the zombie can turn.
So the slower this space you make it the more the zombie will sort of arc around in an arc and head
towards the camera as it's moving forward rather than snapping the zombie to look at the camera which
you could do.
This is giving it a little bit more of realistic sloping motion.
Now in your update you then need to add this.
First of all we need to work out the vict down to the camera so the direction that we're going to sit
for the zombie is the camera's position minus the zombies current position.
This code is going to be attached to the zombie prefab.
We then sit the y direction the up direction of this direction vector to zero because we want it to
be a perfectly horizontal and flat vector.
Doing this Mame's that if you've got a character that has our origin position that's not at the same
height as its goal position then it will still turn on the flat on the horizontal.
Otherwise if you type that out and the origins are in at different heights then your character will
be tipped as it's trying to move exactly up or down towards its goal destination.
So this is a little bit of a trick to make sure that you get perfectly flat direction turn or slip in
your character.
Next what we do is we set the rotation of the character using slip to go from its current rotation to
a rotation in the direction that we've calculated.
And we're not doing that straight away we're doing it little bit by little bit using our rotation spade
multiplied by time delta time.
So what a slip is going to do is turn through an angle little bit a little bit.
Not all the way at once like a little cat does.
OK.
Now we're not pushing the character forward.
There's no transform doctrines like going on.
He noticed that because the character is already moving forward because it's animation is making it
move forward.
Now save this go back into unity.
And where is Al Jill prefab his zombie.
Grab hold your chase script.
Drag and drop it on to the inspector to add it to a prefect and press play to see the behavior.
And when we start tracking over here in the scene of spawning there and they're now going towards the
camera.
Now if I just move the camera up a little bit and then have them spawn in a different location like
here they're still going towards the camera over here.
They're still spawning and heading towards the camera.
And in fact when I get towards the camera they'll just go round and round and round and swarm the camera
app and we've got no way of repelling them at the moment because the bulls don't do anything to them
when they hit.
Before we get around to doing that though you might want to spawn them a little more randomly than in
the exact one spot when the camera is there is I rather them all being here.
You might want them sort of spread out in an arc from that particular vector which means we need to
find an angle that we like and then say that random lay between these Sango and this angle them are
going to spawn as zombies and you guessed that I'm going to leave that as a challenge for you to sort
out and I'll come back in the next lecture and show you how to solve it.