[MUSIC]
Okay, in this video, we're going to shift our own enemy path.
So rather than having to enter all our waypoints on our enemy,
we're going to move it to our scriptable object, our wave asset file.
And also to make sure we don't have to drag in each of these individual elements,
we can just say, what is the path?
Let's drag in the entire path as opposed to having to drag in each individual
waypoint.
So the first step in this process, let's go back over into our waveConfig.cs.
And you can see where we are returning our pathPreFab.
So at the moment this is a little bit unsophisticated cuz we're saying in our
scriptable object here, we're saying in our waveConfig, here's the PathPreFab,
do whatever you want with it.
So let's do the heavy lifting in here where we actually say,
here are the different waypoints within the path.
We wanna be returning waypoint, whatever it is, 1, 2 and 3 or 0, 1, 2 that we have.
So we return the waypoints, not just the PreFab.
So to do that, first of all,
I'm gonna change my method name from GetPathPrefab to GetWaypoints.
And instead of this being a game object, I'm going to create this as a list.
So List, our angle braces and then it's of type transform.
So I'm making a list of type Transfom called GetWaypoints and I will expand this
one out now, rather than having this all on the same line, just make that clearer.
So I've got my curly braces at the top and bottom of my code block.
We're not returning the path prefab anymore.
We want to return the, what are we returning?
Well, we're returning the waveWaypoint.
That is what we want to return.
And how we're gonna do that?
Cuz waveWaypoints don't exist yet.
So let's give ourselves another line or two.
First of all, we'll create a variable, which will be waveWaypoints.
And what would that be?
Well we need to tun this into a list.
So it's a new List of type Transform and that's no coincidence.
And then open, close parentheses and semicolon.
No coincidence cuz we're returning a List of Transforms, so
we need to create this variable which is a new List of Transforms.
Now this is where we use for each loop.
And I told you in the previous lecture, when I was asking you to go through each
of these and I've said for each of these, create a public method.
That's what we need to do when we're looking at the particular
waypoints in a pathPrefab.
We say for each of these, that one and that one, and
that one, do a particular thing.
That's what the for each loop is saying.
So let's start this off by saying foreach.
And then in open parenthesis, Transform, this is how we create our foreach loop.
We first of all say what the type is, and then we give it a variable name or
give it a name, and we'll call this child.
Why is it child?
Well, this is our parent, path is parent that we've created already.
And then underneath the parent we have these children that are the waypoints.
So path is parent and children is waypoints.
So therefore,
we want to be accessing the child, that's why we're gonna call it child.
So for each child and the transform of that child in, and what is this in?
Well, it's in our pathPrefab.
So we're still connecting the pathPrefab to the scriptable object.
So it's in pathPrefab.
And we're particularly interested in the transform.
So we need to say .transform.
And then so for each of these, and
we'll do our open curly braces here, what are we doing for each of these?
For each child that's in our pathPreFab,
particularly getting the transform information.
And just another super, and
I do this all the time when I'm looking to confirm what it is that I'm doing.
If you mouse over pretty much anything,
you'll get lots of information about what's going on.
So, it'll tell us the transform, that it's a .transform, position, rotation and
scale of an object.
Well, we knew that, and we know that this is the pathPreFab and
this is within the waveConfig.pathPreFab, great.
And then the transform is getting the transform attached to this game object,
so this game object, it's getting that transform.
So, just a reminder there, sometimes mousing over things is a really good help.
Cuz my goal here is to teach you how to do this on your own, so you can do more than
just listen to my videos, you can go off and create awesome things yourself.
Okay so that's enough sidetrack.
For each transform child in Prefab, .transform,
we want to say waveWaypoints.Add and this is the cool thing
about using the list is we can add each of these into the list, what are we adding?
So in open our parentheses, we're adding child.
So for each of the children, the waveWaypoints.Add.
So, add the child or
add the transform of that child into our waveWaypoints, which is our list.
And we're using the .Add method in here to add each child.
Okay, so that's our foreach,
hopefully, I know I've explained it about eight different times now but
hopefully that's nice and clear what we're doing with the foreach.
We'll save that up and let me just get rid of my lines here.
What we are returning when we are calling this GetWaypoints as a public method,
what we're returning is the waveWaypoints within our path prefab.
Okay, we'll save that up.
Now what we need to do to test that this is all working,
is to jump over to our EnemyPathing script.
Within here, we're gonna make a couple of slight tweaks.
So what are these slight tweaks, I hear you ask.
First of all, we need to SerializeField.
And we need to have a way of connecting our wave configuration.
So I'll say WaveConfig is the type and that's our scriptable object, WaveConfig.
And we'll call this waveConfig as the variable, semicolon.
We still need our list of waypoints but we don't need to serialize this.
We're no longer going to be dragging in each of these various waypoints.
These other parameters we can leave as is, which is cool.
Within our start, we need to do something,
we're going to say waypoints = what does this equal here?
Well, it's gonna = waveConfig.
Can you guess it?
It's gonna be GetWaypoints.
Open and closed parentheses, semicolon.
Now, let's save this, jump back over into Unity.
I'll give you a challenge at this point, actually,
just to make sure all this works.
Hopefully, it works.
The challenge is to create a new path, make a new path prefab with different
waypoints, so that we can make sure that the new one is working properly.
To add a new Wave Config asset file, so a second asset file.
And then to assign the new config to our enemy prefab, click on Play and
to make sure our enemy is following our new path.
Okay, so jump into that challenge and
I'll see you back here when you've got that done.
Righty-oh, so we've already got Path 0, I'm gonna duplicate Path 0,
it is now called Path 1, this also has three waypoints.
I'll be a little bit clever here and I'll duplicate and we'll make four waypoints,
just to be super clear that our system is setup right,
click on Waypoint 0, put that all the way to the top.
And just a reminder, if you lose your points, then you can come up to the top
right or up in your Inspector and select the icon that you want for your waypoint.
Sometimes I find they come and go, I'm not sure why but minor there at the moment.
So start at the top right into the middle and then down closer to the player and
perhaps then heading towards the offscreen.
We have this path, we need to make this a prefab, so I'll drag path1 down
into my Paths folder, that is now a fully official prefab excelente.
What do we need to do next from our challenge?
We need to add a new wave configuration file.
So within our waves, again I'm gonna duplicate.
We have a wave 2, click on wave 2, you can see that its looking for
path prefab path 0.
I need to find my, so I click on the selecter, next to path prefab,
find path 1, excellent, the enemy prefab is the same.
And then the last step, assign the new configuration to our enemy prefab.
Okay, so Prefabs, Enemy, this is a little bit manual,
we'll be changing this up, I will be iterating on this as we go.
But for now, we need to find the waveConfig and
to say you are now wave 2 that I want you to execute on.
And then we'll click on Play, we'll see if our enemy follows our new path.
Up the top and then down or below.
Great, so enemy is following the new path.
We've sucessfully set up a second path.
We've hooked it up, so that we don't have to put in each individual waypoint,
that our enemy is finding its own waypoints.
We just have to tell it what path to look at.
So, great work, still plenty of work to do.
But we're making awesome progress.
See you in the next video.