Now that we've got our player moving around the screen, it's time to start thinking about adding some
enemies.
And the way we're going to do this is going to involve a few different scripts talking to each other.
So before we just go ahead and start coding things up, let's actually talk about how our enemies are
going to work and what script we're going to need.
Now, our enemies themselves are going to have a few different requirements.
They're going to need to be able to cause damage to the player, either through collisions or through
shooting at them.
And rather than just having a single type of enemy, we're going to have different enemies with different
attack behaviors.
So they may shoot faster or slower or they may even have different projectile types.
The enemies are going to affect the player's score later on, so if they get destroyed, they're going
to increase the player's score.
And we may also add a mechanic in there to lower the player's score if they manage to escape unharmed
and the enemies are going to spawn into our game in waves.
Now, if you're not familiar with that term, let's have a look at what waves actually are.
Well, you can kind of think of them as self-contained moments of gameplay.
So we can see an example of a wave here where we've got two enemies following a set path.
So for our game, a wave is going to spawn a certain number of enemies over a given amount of time,
and all enemies in that wave are going to follow a set path once that wave has finished.
So either all of the enemies have been destroyed or reached the end of the path.
We can then go ahead and generate the next gameplay moment or the next wave.
This could be the same as the first, or it could involve different enemies following a different path.
So that's the basic mechanics of what we want to happen.
But to get all this working, we're going to need a few different scripts working in unison.
So next up, let's look at the scripts and their various responsibilities.
First of all, we're going to need a wave config file and this is going to be a script object.
This script will object is going to tell the wave which enemies need to be spawned.
It's going to tell them the path that needs to be followed.
And it's also going to control things like the time between enemies arriving within that wave.
So they don't all just arrive at the same time and it's going to control the enemy movement.
SPEED So we may have certain waves where the enemies are quicker or slower because all of this is contained
within script or objects.
We're going to need some scripts to actually use this data and the first is going to be the enemy spawn.
This, unsurprisingly, is going to spawn the enemies into our game at the start of the path, but it's
also going to dictate the order of the waves.
So if we have multiple waves in our game, which we certainly will do, it's going to tell us which
one to generate first and in which one to generate after that one has finished.
And finally, it's going to be in control of the time between those waves.
So that takes care of bringing the enemies into the world, but we also need to get them moving.
This will be the job of an enemy.
Pathfinding script and all this is going to do is tell the enemy to move to the next waypoint in a path.
Now, we're obviously not going to get all of this done in one lecture, so let's just start by getting
our enemy moving along a fixed path.
And to create a path is actually incredibly simple because we're going to be using a waypoint system.
So in our hierarchy, let's right click and create an empty game object and then reset the transform.
And we'll call this object path zero because we very well may have more paths in the future.
Then within this empty path object, let's right click again and create another empty object.
And we're going to call this waypoint zero.
And this time I've placed the zero in parentheses so that when we duplicate this object, it will automatically
index these for us.
Before we go ahead and do any duplication, though, the general idea is that we're going to have multiple
waypoints in this path and then the enemy is going to move from one to the next.
Because these are empty objects.
We can't see them in our scene, which makes them very difficult to work with.
So to fix this, rather than adding a sprite, which we would see in both our scene and our game view,
what we're going to do is head up to the top of our inspector here where we've got this little cube
icon.
If we click on that, we then have some icons to choose from.
I'm going to pick the yellow diamond so you can see what's going on here, and we can see that in our
same view.
We've now got this little yellow diamond.
If we zoom in, we can see it a bit better.
But if we look over on the game view, that little icon doesn't appear anywhere.
So we don't like to have to worry about switching these things off in the game view.
Now if we zoom back out so we can see our full game view again here in our scene, let's go ahead now
and duplicate some of these waypoints.
So just control the and I'm going to create five in total.
All we have to do now is arrange them into a path that we want.
I'm going to go with a similar path to what we saw in the slides, but feel free to create whatever
kind of path you like.
So with my path set up, I've got a nice sweeping curve here and notice that the start and end points
are off of the screen.
This is so the enemies can both appear and disappear off camera so that we don't get any abrupt changes
where the enemy just pops in or out of existence.
With our path set up, let's go ahead and prefab it.
And rather than putting it into our prefabs folder because we're going to have a few of these, let's
create a new folder specifically for our waves and our paths.
So this will contain all of our paths and all of our wave config data later on.
Let's go ahead and drag our path into that folder.
And now let's look at how to get our enemy moving.
We set the data for things like the path and the enemy movement.
Speed would all be contained within a script object.
So let's go ahead and go into our scripts folder and create this script object by right clicking, going
to create and then C-sharp script and we're going to call this the wave config.
So now we're not going to write this entire script or object in this lecture, but we are going to get
enough information in there so that we can move our enemies.
This is a script to objects.
We don't need the start and update method, and we also need to change our mono behavior to be a script
object so that we can create new wave configs in unity.
Let's go ahead and add the Create Asset menu attribute to the top.
I'm going to set the menu name to be wave config and I'm going to give it a default file name of new
wave config.
Now let's get our economy moving along the path this script will object is going to need two variables.
These are all going to be serialized, filled, and the first is going to be of type transform and it's
going to be the path prefab that we've just made.
So this will contain all of the waypoints that we need.
Next, we're going to have another serialized field of type float.
And this is going to be the move speed for our enemy.
And while we're here, let's give it a default value of around five.
Now because these variables are private, being there in a script to object, we're also going to need
some getters to access them from outside of this script and for the move speed, that's going to be
fairly straightforward.
We're just going to have a public float called Get Move Speed, and this is going to return the move
speed.
But for the waypoints in the path, things are going to get a little bit more complicated because we
don't really need the container that those waypoints are in.
We want the waypoints themselves.
We're also going to want a quick way of grabbing the very first waypoint in the path as well.
So let's start off with that one because it's a bit simpler.
So our method for this is going to be public and is going to return a transform and we're going to call
this get starting waypoint.
And the starting waypoint in our path is going to be the very first child of this container so we can
return the path prefab dot get child at index zero so that will return the very first child for us.
Next, we want a way of returning a list of all of the waypoints.
So let's create a public method.
And this time we're going to return a list of transforms and we'll call this one, get waypoints for
this.
We're going to need to loop through all of the children within our path.
Prefab parent object.
And you may be thinking to yourself that a for loop is going to be ideal for this task and you're certainly
not wrong.
However, what we're going to do instead of using a for loop is we're going to use a for each loop instead,
which works very similarly, but the syntax is slightly different.
So how does a for each loop work?
Well, first of all, let's remind ourselves of how a for loop works.
And in the context of what we're doing, we may have a declaration, something like four int equals
zero.
So initializing our iterator at the first index of our path.
And then while I is less than the child count of the parent prefab, we want to go ahead and grab the
child and then increment our index by one.
And as I said, there's absolutely nothing wrong with doing it this way.
However, we could instead opt to use a for each loop which operates exactly the same but has a slightly
cleaner syntax.
So to do this exact same thing with a for each loop we could say for each child transform.
So child is a variable in this case and it's of type transform in our parent.
So in our case our path prefab, then we want to go ahead and do stuff.
So let's head back over to our script and to return a list of all the waypoints in our path.
Prefab First of all, we're going to have to create a new list of transforms and we'll call this waypoints.
And then we'll initialize that to be a new list of transforms.
Then we're going to go ahead and loop through all of the children of our path prefab.
So using our for each syntax we're going to say for each transform child in path prefab.
So we're going to take our path prefab and loop through all of the children and store them as transforms.
And every time we do this, we're going to take our waypoints and add the child.
Once we finished doing that, we've gone through all of the children.
We can go ahead and return our list of waypoints.
So let's save our script to object.
And that should have everything we need for now.
And to stop this lecture from getting too long, we're actually going to stop things there for now.
And we're going to write the Pathfinding script in the next lecture.
So well done.
Forgetting our wave config script object started and I'll see you in the next lecture where we implement
our enemy movement.
So I'll see you there.