In this lecture, we're going to pick up where we left off last time and create our pathfinding script
to get our enemies moving.
So let's jump back into Unity and create our next script.
Let's right click create a new C-sharp script called Pathfinder.
And then over in our prefabs, let's attach this pathfinder to our enemy prefab so we can add our component
and find our Pathfinder script.
Let's open that Pathfinder script up.
And what this script is going to do is set our enemy to the first waypoint in our list and then iteratively
move it towards the next waypoint that it hasn't visited yet.
Once it reaches the end of the path and there are no more waypoints in our list, we can go ahead and
destroy our enemy.
So let's clean up the comments above our start and update to begin with.
For all variables we're going to need reference to a wave config script will object.
So let's create a serialized field of type wave config S0 and call this the wave config.
And we're also going to want to store the waypoints for our path.
So let's create a list of transforms and we'll call it the waypoints.
And this will be populated from our script object.
Lastly, let's keep track of which waypoint we're currently on.
So let's have an integer called Waypoint Index and we'll default that to zero.
So it starts at the first waypoint in our list.
In START, we want to populate our list of waypoints so we can say waypoint equals the wave config dot
get waypoints and then we want to move our enemy to the first waypoint in the path.
We can do that with transform dot position equals the waypoint at waypoint index dot position.
Then in update every frame, we want to move closer to the next waypoint in the list.
So in update, we're going to use a method that we're going to call follow path and then under update.
Let's actually create this method.
And in here we want to say that if the waypoint index is less than our waypoint account.
So if we're not at the last waypoint just yet, then we want to move towards the next waypoint in the
list.
To do this, we're going to need to know where we are, where we're going, and how fast we're moving.
Well, we know where we are with our transform position.
So let's think about where we're going and create a vector to called target position.
And we're going to set this equal to waypoint at the waypoint index dot position.
Next, we want to know how fast we're going.
So let's create a float called Delta.
Because this is going to be the distance that we're going to be moving each frame.
And we can get that from our wave config dot get move speed.
And to make it frame rate independent, we're going to multiply it by time dot delta time.
To actually move our enemy.
We need to set its transform position for this frame and for this we're going to use a method called
move towards.
We can find this method if we type vector to move towards.
And we can see from the tooltip here that it requires three parameters.
It requires the current position, the target position, and the maximum distance delta.
And it moves the current point towards the target.
So our current position is our transform position.
Our target is going to be our target position.
So the position of our next waypoint and our maximum distance delta is going to be our delta value that
we just calculated.
Finally, once we reached the next waypoint in the path, we're going to need to increment our waypoint
index.
And we can do this by saying that if our transform position is equal to our target position, then we
want to go ahead and take waypoint index and increment it by one.
Now this is giving us a red squiggly error.
And if we hover over it, it's saying that it's having trouble checking a vector three our position
versus a vector to our target position.
So let's fix that very quickly and we can just change our vector two here into a vector three for our
target position.
That removes our red squiggly and our script should now compile.
But before we go ahead and test it, there's one more thing we need to add here.
And that's what happens when we get to the end of our path.
So our if statement will walk us through all of our waypoints, and then if we get to the end, we can
say else destroy the game object.
So that will remove our enemy from the game once it reaches the end of the path.
Now let's go ahead and save our script and jump back into unity.
We've already preformed our path so we can go ahead and delete that from our hierarchy and then on our
enemy we need a wave config file.
So in our waves and paths let's right click go up to create and we should have a wave config file here,
which I've incorrectly spelt.
So we'll fix that in just a moment.
This creates our new wave config, which let's just call it wave zero for now and this needs a path
prefab which we can drag in and an enemy movement speed which we can leave at five four.
Now with our config file setup, let's again go and find out enemy prefab.
And attach our wave config file.
Now, if we go ahead and hit play, we can see that our enemy nicely moves between each waypoint in
our path, and when it gets to the end, it gets destroyed.
So congratulations on making your enemy move.
And now, while I go ahead and fix my spelling mistake for my wave config file, I think it's time for
your challenge.
I want you to go ahead and create at least one new path for the enemies to follow.
And then I want you to go ahead and create a new wave config file that uses your new path.
So pause the video now and give that go.
Okay.
Welcome back.
So while you've been away, I've gone ahead and created a new path, which is just a mirror image of
the path I created earlier.
And to create a new wave config file, let's go up and create a new wave config with the correct spelling
this time.
Let's call this wave one and I'm going to attach my new path.
Now, if we head back into our scene, we can go ahead and duplicate our enemy for now.
Attach the new wave that we've just created.
And if we hit play, we should see two enemies following distinct paths.
So everything seems to be working great.
And in the next lecture we're going to expand on what we've done here and start creating those enemy
waves.
So I'll see you there.