[MUSIC]
In this video we are gonna be making our enemy move on our waypoints and
then destroying when it gets to the last waypoint.
Let's jump in and get started.
Okay, so the code we're going to write will start off by saying go to the first
waypoint so we need the position of the enemy to be the first waypoint.
And then each frame it's going to say,
head towards wherever the next waypoint is.
So move, move, move, move.
And when you get there or check to see if you've got there yet.
And when you've got there then go to the next waypoint in our list.
[COUGH] So that's the foundations of it.
We're gonna use a really cool method called MoveTowards.
And MoveTowards takes in three parameters.
It needs to know the current position, so in other words,
this is the current position where the enemy is right now.
It also needs to know the target, and it needs to know the maximum distance delta,
in other words, how fast to go.
And if we're right out our flow as some pseudocode, then it would look like this.
And I encourage you to write out pseudocode when you're trying to work
through a problem to say, okay, well, what are the elements?
What's the process?
How does it flow in terms of what I'm trying to do?
For our particular challenge at the moment, we start off by saying,
if I haven't yet reached my last waypoint then do these things.
And we're gonna say, otherwise, this'll be the end of our code block.
At our pseudocode here, we'll say otherwise destroy yourself.
So we wanna know if you've not got there yet, then go move.
And we're gonna use MoveTowards the target waypoint when we're moving.
And then we need to check if we've reached the target, so
this particular waypoint we're going to.
If we have reached that waypoint, then we want to increment our target waypoint.
So to go from waypoint one to two, waypoint two to three, and so on.
And then, if we have reached our last waypoint, we destroy the enemy gameObject.
I wanted to show you that rather than just diving straight in, and
typing stuff into my script.
But we will dive in now and start typing stuff into our script.
And the script is the EnemyPathing.cs.
So what we need to do at the top is give ourselves a couple
a variables to be using.
First of all, we need to have an integer, which will be for our waypoint index.
And we need to initialize this at 0.
Basically, the waypoint index will be what index am I or
which waypoint am I currently working toward.
And we started at 0 to say that's the starting point.
Why is it 0 and not 1?
Well, a list, just like an array, starts at 0.
[COUGH] Okay, we also need and I'll put this,
I'll keep my serialized fields together here.
I'll serialize another field for a variable which we typed float and
this is gonna be our move speed to say how fast do we want our enemy to be going and
I'll start this at say 2f.
Okay, so we've got our three variable.
Now, let's have a look at our pseudocode,
where you need to do an if statement that says, if we've reached our last waypoint.
Actually, not before that in our start, we actually need to say where to start,
we wanna do our starting point first of all.
So where will the starting point be?
Well, the starting point is gonna be transform.position.
So the transform.position of this particular thing that the script is on,
our enemy pathing script is on our enemy.
So we're saying, the transform position of the enemy, in other words,
the instance that this script is on.
Equals, [COUGH] well, here we want to get some information from our list.
Basically we wanna say, the transform position of the first entry,
or the 0 index in our list.
So we'll say waypoints and we access, not waypointIndex,
we wanna say waypoints, if I can type it properly, yes, that's right.
And then we use the same open and close square brackets we used for
an array, this is a list we're using.
And we want to say for waypointIndex,
no, having a bit of a mess with this,
waypointIndex.transform.position;.
So our position should be the transform position of whichever the current
waypointIndex is, and we know we're starting waypointIndex at 0.
Okay, so basically go to position 0.
Now, in the update is where we want to move.
And this is why I was looking at my pseudocode in here.
If we haven't yet reached our last waypoint,
how do write that as an if statement?
So type if(waypointIndex <=,
what's it gonna be less than or equal to?
It's gonna be waypoints.count.
The reason it's count is because we're using a list.
If we were using an array, we would say .length.
Because an array is fixed, we know the length,
whereas we actively have to count for our list.
Then, we need to say -1 to make sure that this will continue unless we
are definitely above and beyond.
So if we've gone higher than the number of items in our list.
So that's why we do the -1.
Open up our curly braces and then we need to create two things in here first of all.
If we go, jump back over into, sorry, I'll just have a look.
Yep, into our MoveTowards, we need to establish a current position, we need to
establish a target position, and we need to establish a maxDistanceDelta.
So two of those, we need to construct a variable for.
So we'll say, var targetPosition =, while we're looking for
the position in the waypoint we're moving to.
So waypoints, what's the current active index?
That will be waypointsIndex,
and with .transform.position.
So what is the position of the current way point?
We're heading towards would be the target position.
And then we need another variable which would be var movement.
We'll say, movementThisFrame, that will be the speed we're moving at.
We're going to times this by time of delta time, so this will be based upon a frame.
And when we frame right independent, as we've talked about just recently.
We'll say this equals our recently created moveSpeed,
everything is recently created, moveSpeed * Time.deltaTime;.
Okay, so we're establishing, this is where we want to go to, and
this is how fast we want to move.
And in the next line, we'll need to say the transform.position of
the thing that we're on, in other words, the enemy, equals.
And to finish this with our Vector2.MoveTowards,
I'm gonna give this to you as a challenge.
And the challenge is to write or
finish this one line that will move our enemy towards its target waypoint.
So that's the challenge.
Let me just leave on the screen for you the URL, here it is,
that has the MoveTowards.
So you can see that, again, here is the construction of it.
This is the signature.
And we have everything that we need.
So far we've created all of the elements.
So finish off that statement and I'll see you back here in a moment.
Okie-dokie, so what do we need?
transform.position = Vector2.MoveTowards, that's our method.
And we're taking in three things in here, and for our signature,
we need the current.
Well, the current we know is transform.position, that's where we
currently are, transform.position, we've already created targetPosition.
And then we've also already created MovementThisFrame;.
Okay, and this is big line, so I'm just going to put an indent, or
a return after MoveTowards and before our parameters.
Okay, just to tidy things up a little bit, we having our MovingTowards.
Now, what do we need to do?
Back over to our pseudocode here.
We need to check to see if we've reached the target, and
if so, to increment the target waypoint.
So the way we do that is, we need another if statement, and it'll be if, what?
Transform.position is the same as or equals, but
it's equals-equals, targetPosition.
Then, what do we want to do?
Open our curly braces, we say, waypointIndex needs to increment by 1.
So that's waypointIndex++;, I'm gonna put a little line in here
just to keep it nice and clean in terms of our space.
You don't have to, but that just helps me see what's going on.
And then as the very last step in this whole process we need to align ourselves
where our if is need to be putting in an else.
What we're saying in here is so,
if we still have a valid waypoint, then do all the moving.
Otherwise, or else, we will say Destroy, what are we destroying?
We're destroying the gameObject;.
Okay, and as a very final step I will highlight all of my
code within update to there.
I'll right-click and just factor this out,
so it's not sitting there in our update method looking all busy.
And we'll call this Move as our method.
Okay, so now when we look at update, we're saying, move, and
if we wanna do other things in update it would be nice and clean and
our whole Move code is sitting just below.
Okay, let's save that, jump over into Unity, check that this is all worked.
It doesn't really matter where our enemy is on our screen.
When we tidy all of this up, we won't actually have this enemy on the screen.
We'll be instantiating an enemy from our prefab asset, but for now,
we're going to use this one that's on the screen that we've dragged into.
We've instantiated ourselves by dragging it in.
We have the way point on there, we have the three way points in our script.
So let's click on Play and see what happens.
It jumps to the first point, moves to the second, and then goes off to the third and
then it should [SOUND] destroy itself, fantastic.
If you wanted to make your enemy move more quickly,
then we could change the move speed to say, 5,
not 56, to 5, click Play and then it should move faster through that flow.
There we go, boink, boink, excellent.
So there it is, there's the foundation of our pathing.
And our waypoint system, you can lay down as many waypoints as you want.
You can have a nice elaborate path that zigzags through the screen.
We're gonna do it coming off screen, do some action and
then off screen again after the enemy goes away.
So this is really cool, this is a great foundational piece.
Foundational piece that we can build the rest of our game on.
Great work and I'll see you in the next lecture.