In this lecture, we're going to look at creating a waypoint system that can be navigated with the aStar
algorithm to begin download the resource file that comes with this lecture.
And in it you'll find this scene with the tank.
If you've done any of the other unity provided tutorials, you'll have seen these models before, and
I use them quite often because while they're nice to look at and they're convenient and easy, so we
don't put our own map together.
Of course, if you want to put your own map together, go for it.
That's fine.
Now we've got a map and we've also got a little tank.
The tank will be our NPC character.
What we're going to do is place some white points around the map and then we're going to link them up
with edges and put some buttons on the screen to give the tank orders of where to go to.
The first thing we'll do is place some waypoints.
The important thing to remember here is that there's no physics or collision detection that will be
in this project, which means you have to place waypoints where the tank can move.
So it'll be the paths in the project.
I've created a waypoint game object asset here, so this prefab, if I drag and drop it into the scene,
you'll see that it automatically gives itself a number and each time you add a waypoint, it will count
up for you.
So this is so you can keep track of which waypoint is which one without having to click on it in the
hierarchy each time.
So let's just zoom out.
I'm going to play some waypoints around this map to get to the helipad, which is down here, and one
to get over here and to go around this building.
But you can play somewhere if you like, so I'll just go ahead and do that quickly.
With all the waypoints in place, they will become the nodes in our graph.
So we need to now determine where the edges are going to be.
And the edges are the lines that connect the nodes and will tell us where the tank can travel between.
As I said at the beginning, these waypoints are also going to specify where the tank can't travel,
and that is that we don't want to put any paths through geometry.
So it's working as a collision avoidance system as well.
And so you can put waypoints like this through buildings if you want your NPCs to travel around in there
and look like they're intelligent because then they're avoiding obstacles.
So in this case we can connect waypoint one with two and two with three and one with three.
Anywhere where there's a clear line of sight, we can join them so we can't get from here over to number
seven at the back going that way.
So we'll put lines here and here and I might even put a one way path that goes up to here so that the
tank, if it is over here and wants to go back, has to go around this way.
With all your paths planned out, you might like to write it down so that you've got a reference for
when we go and put this information back into unity.
Now before you do that, you might want to rename all of your waypoints and number them properly.
So I was doing a duplication of each waypoint and moving around on the map, and that means that unity
gave them odd numbers like WP 003 was actually WP 002 and in brackets one.
And then there was in brackets two, in brackets three.
But instead you might want to number them like this.
So it just seems a little bit more logical.
And then you will see those names properly appear on your map so that you can work out your edge list
between all these according to the graph that you just came up with, with the lines between them.
Now it's time to start creating some code.
But just before we do, we need an object in the scene which is going to manage all the waypoints essentially.
So in the hierarchy, right click and create an empty.
And we're going to call this object a WP manager.
So this is our waypoint manager on that.
We'll put some code.
So let's go over to the project and create a new C-sharp script and we'll also call that WP manager.
It doesn't matter if they're not the same.
I just like to keep it consistent.
Now with that WP manager, I'm going to drag and drop it on to our empty game object.
Right.
So when you've got that code, let's open that up.
Now, let me just make this font a little bit bigger for you.
So I think I turned it down the other day.
Okay, so in here, what we're going to do, first of all, is going to create a little structure at
the top to help us make the inspector look good when we go to link up all of these nodes or waypoints,
I should say, between each other with the edges or the paths.
So let's create a system dot serialize able and then a quick struct.
So public struct and we'll call it link.
So this defines a link as in the edge or the path between two nodes or waypoints.
So in here we will have public enum which will set a direction for and that direction will specify whether
the path is one way or two ways.
Now most of the paths we've put in are two way paths.
In fact, I think all the ones we are, you don't want it to go into sort of a dead end.
If it was to cycle maybe around that factory that's in one part of the map, you maybe you could have
a uni directional path that went around that so that you could only go one way around that.
Okay, let's put public game object node one, public game object node two.
And then we want a public D by R, direction d, i r.
So that's going to hold whether it's a unique or bidirectional link between them.
All right.
So we've done that.
Now, what we're going to do is to put some code down in the actual class itself.
So at the top here, we will create an array of game objects.
So public game object array, which will be our waypoints.
Then we'll have a public link array, which will be our links.
Okay.
Now once we've got this, we can then set up our graph.
Now we want to use an official graph library for this.
So we'll write our own quickly, which means we have to get out of here and start doing that instead.
So we're just going to save this code and I'm going to leave this lecture here so that we can look at
the graph code separately in another set of lectures so that it's sort of one defined area.
And then we'll come back to this.
Okay, so I'll see you in the next lecture.