[MUSIC]
In this video we're going to start to the process of giving our enemy a path to
move along.
We'll be creating a list,
which we will use to store a whole bunch of waypoints for our enemy path.
So let's get started in this process, over in your hierarchy, right-click
create a game object, an empty game object, which we will reset the transform
of straight away and we'll change the name of this to, Path open parenthesis 0.
Next, we will right-click on Path, Create Empty,
and I will name this Waypoint parenthesis 0.
Okay, before I duplicate waypoint,
I'll show you a little trick here which I think is pretty neat, so
clicking on waypoint I'm going to move that a little bit up to the right.
When we're doing our proper path we'll have our path start from off screen and
then come into our world, zing around a little bit and then go off screen, but for
now I'm gonna do something very simple just so
that we can test that it'll works out okay.
So we can see when I'm clicked off waypoint that it disappears,
this is one of the problems of having an empty game object all it has is
the transform information.
When we click off it there's nothing to help us see where it is,
we can resolve that by up in the Inspector, top right up here,
you can see a little cube, a blue, red, and green cube.
If you click on that, left click, there's some icons we can use to represent
our game object, I'm gonna choose a yellow diamond.
Now in my scene view I can see a yellow diamond at the place where the game object
is to represent it, it doesn't show up in game view this is only in the scene view.
Okay, now I've got one waypoint I'll duplicate couple more times,
you can see our nice naming system here, it's nice and neatly one and then two.
I'll click on waypoint 1, move that somewhere into the middle and waypoint 2,
move that somewhere over to the left, so
I've got a very simple path that's just going from right to left.
Next we need a way to be able to store the various waypoints, so
when you say this is the first waypoint, that's the second waypoint,
that's the third waypoint and so on.
And this is where I wanna talk to you a little bit about lists and
a list is really similar to an array and let me remind you about array.
So this is a slide from earlier in the course where we covered that an array
stores into a variable, multiple elements of the same type.
This is how we create our array and this is at the position 0,
position 1, position 2 and so on, so that's an array.
So a list is very, very similar to an array in many ways,
it stores values into a position in the array,
the big difference is, sorry into a list, the big difference is
that a list is dynamically sized while the array is fixed in size.
So the real value for our list is we could could get there and say, okay,
currently we've got five positions, five elements in our list but
we want to remove one of them or add one and our list allows us to do that.
There's a lot more things that we can do with lists,
there's more flexibility, so there's cool things that we can do.
If we have a look at a direct comparison here, list versus array, sorry,
just back to the previous slide.
The way that we create a list is we type the keyword list and
then we have our angle brackets, we put in the type.
In our case we're saying it's an enemy type and
then we write the name of our list that we wanna create.
Okay, so just a quick comparison with the collection size,
arrays tend to be fixed there's some slight exceptions to that, but
they are fixed, whereas lists are variable.
In terms of performance arrays tend to be faster, list can be fast, in our case,
unless we're putting in lots and lots and lots of values,
which we're not we're just putting in a small amount,
then list, we're not gonna see a performance problem at all with list.
But it's something if you're dealing with tons more data
then that might be an issue.
Ease of use, well they're pretty easy, they're very,
very similar, list has kinda one more word to type when you're creating it.
The flexibility is low with arrays, but tons of flexibility with list,
a lot more that we can do with it.
And then in terms of how you create it, an array, it's the type,
and then square brackets, and then name and
list as I showed you a moment ago, list angle brackets type, and then name.
And if you've been through our Unity 3D course, you might recognize this slide,
and this is Ben's, I stole this slide from Ben.
If in doubt whip a list out Ben is a fan of both lists and
whipping out, so that's Ben's words of wisdom there.
I'm gonna flow straight into the challenge for
you in this video to create a list with Path Waypoints, so,
add waypoints for enemy to follow, which I've just done in my scene here.
So, if you haven't done that, now's a good time to do that and
then to create EnemyPathing.cs as a new script and
attach that to the enemy prefab, that's the way we'll be doing this for now.
And then to serialize in that EnemyPathing.cs, serialize a list,
named waypoints, using the structure that I showed you a moment ago in terms of
how to create a list and then to add the waypoints to your list in the inspector.
Okay, so there's your challenge, you should have all of the Intel you need to
do that, so just one last reminder of how to construct the list, so
List and then angle brackets Type and then name.
Okay that's how you create that, and then here are the points of the challenge,
jump in take that on and I'll see you in a moment.
Okay so I will grab my enemy prefab, add a script to this,
which I will call enemy pathing, I've already been prototyping this so
enemy pathing is typed in already hit, Enter, Create and Add, very good.
And then in my assets I will, click on Assets, just drag that,
good to tidy that up put it into my scripts folder and
open up enemy pathing in Visual Studio.
Enemy pathing at the moment doesn't have anything in it,
I'm going to jump right after the very top here and create my list.
So serialized field, I wanna be able to access it in my inspector, and
thn list, open our angle brackets, and what is the type?
This might be something that stumped you as you went through the challenge,
the type we're looking for is the Transform, Transform so
well done if you got that figured out and then we will call this waypoints.
Why is it transform, why is the type transform?
Well, we're looking for the position,
we're looking for really anything we need out of this way point is where are you?
And we can see that this particular waypoints transform is 4.17 on the x and
3.3 on the y, so this is why we just want to know the type is the transform.
Okay, we'll save up our script, back over into Unity.
So one way I could do this is clicking on my enemy, I can see I have my waypoints
here a could drag the waypoints from my scene into the list.
And that would connect up the enemy that's in my scene with the path that's in my
scene, however, that's not very scalable because then when we go to add
more enemies we have this weird connection to this path that's in the scene and
we don't have it from our prefab.
So it's better for us to connect prefabs to prefabs, this is one
thing that I didn't tell you in the challenge but we'll go through this now.
I'm gonna in assets, I'll create a new folder, right click,
create folder, if you did it the other way, if you did instance to instance or
from scene to scene, that's totally fine as well, that's cool.
But I wanna show you a way that's gonna give us a bit more power in the future.
So I will call this paths, open up paths, dragging my first path, okay,
now that I've dragged it in, I'll leave it in my scene, so I can visualize it.
But I don't need it in my scene anymore,
because I'm gonna connect prefab to prefab where I find my enemy prefab.
So Prefabs, Enemy, I will lock the inspector, and then find my path,
open up the path, grab the three waypoints and drag those over into Waypoints.
Now we have a connection between our enemy prefab and our path prefab.
Okay, so, that’s all for connecting up our paths to our enemy,
the enemy now knows where it needs to go.
In the next video we’re gonna dive into the meatier,
chunkier topic of actually having the enemy move along the path.
So, I’ll see you in the next lecture.