WEBVTT

00:06.490 --> 00:11.740
In the previous lecture, we started putting together this application with waypoints in it.

00:11.740 --> 00:20.560
So we're going to now create a graph class that will allow us to apply the algorithm and get our little

00:20.560 --> 00:25.770
tank moving automatically between these points and calculating its own path.

00:25.780 --> 00:33.400
So in the assets, what I'll get you to do is to create a folder called graphs.

00:33.880 --> 00:39.250
Now open that up inside here we want to have three C-sharp classes.

00:39.250 --> 00:43.000
So C-sharp, let's create graph.

00:43.300 --> 00:45.100
Then we want another one.

00:45.100 --> 00:56.020
So we're going to create C sharp, which we will call Node and one more create C sharp, which is edge.

00:56.830 --> 01:04.210
Now as you've seen previously in the course, a graph is made up of nodes which are basically our waypoints

01:04.210 --> 01:08.320
and edges, which are the paths that go between those.

01:08.320 --> 01:16.000
So these three classes are going to allow us to hook those things up as well as run and a star through

01:16.000 --> 01:16.360
them.

01:16.360 --> 01:23.140
So let's start by opening up the simplest of these, which is our edge.

01:23.230 --> 01:29.620
So inside of the edge, what we're going to do in here is actually get rid of it being a mono behavior

01:29.620 --> 01:35.830
because we don't need it having any special mono behavior features at all, which means it doesn't need

01:35.860 --> 01:39.040
a update or a start up here.

01:39.040 --> 01:43.450
We're going to have public node, start node.

01:43.450 --> 01:46.600
Now we haven't defined Node, but we will come back and do that in a moment.

01:46.690 --> 01:50.440
Public node and node.

01:50.650 --> 01:54.970
So that's the path's starting node and ending node.

01:54.970 --> 02:00.070
So the ones that you can actually travel in between, then we'll create a constructor.

02:00.070 --> 02:10.360
So public edge where we will put a node from and a node two and then inside of here we will just set

02:10.810 --> 02:23.470
start node equals from end and node equals to okay, that's pretty much it for your edge class, so

02:23.470 --> 02:24.520
just save that.

02:24.550 --> 02:27.880
Now open up your node class.

02:28.540 --> 02:36.760
In the node class, we're going to again get rid of our monitor behavior and get rid of our leftover

02:36.760 --> 02:38.950
unity methods that come with it.

02:39.220 --> 02:42.180
Now, a node is like a waypoint.

02:42.180 --> 02:48.460
It's a position in your map and it can have these edges coming out from it.

02:48.460 --> 02:56.050
So it can have as many edges as it needs to have public list edge.

02:56.050 --> 03:05.920
So make a list of all of these edges, which will be our edge list equals new list edge.

03:06.700 --> 03:12.190
Then we want a public node path equals null.

03:12.190 --> 03:21.970
So this will allow us to define or keep hold of a path that's going from our node game object I.D. Now

03:21.970 --> 03:29.950
that's going to be the game object that is at our path location or sorry, our node location, I should

03:29.950 --> 03:32.920
say the one that's representing where it is.

03:32.920 --> 03:37.750
If you're using this graph in a different kind of situation, then obviously you wouldn't have a game

03:37.750 --> 03:39.040
object associated with it.

03:39.040 --> 03:40.420
You could have something else.

03:41.080 --> 03:47.110
Now we want public float its x position.

03:48.990 --> 04:01.740
Public float it's why position and you guessed it public float for the Z position to help us calculate

04:01.740 --> 04:07.080
where they are, the distances between them and where our tank has to move to, when it's going to go

04:07.080 --> 04:08.130
to one of these nodes.

04:08.370 --> 04:08.880
Okay.

04:08.880 --> 04:19.020
So we'll also for the aStar algorithm, have to put together a float for the F, g and H values of the

04:19.200 --> 04:21.750
star algorithm that we'll use later on.

04:21.750 --> 04:28.410
And we want a public node came from now.

04:28.410 --> 04:30.930
This is going to store another node.

04:30.930 --> 04:40.050
So if our character has moved to a particular node, then this is where they came from to get there.

04:40.320 --> 04:49.230
Okay, so now we want a public node constructor, let's pass through the game object, AI, and then

04:49.230 --> 05:00.240
inside of here we will set ID to equal AI and that's our game object x pause is going to equal AI dot

05:00.240 --> 05:16.290
transform position x y pause will equal ai dot transform dot position dot y and z pose will equal AI

05:16.530 --> 05:25.890
transform dot position dot z and then path initially will equal null.

05:25.890 --> 05:30.120
This is something that's going to get constructed as we run through with the a star.

05:30.420 --> 05:40.830
Then down here will create another public game object, get ai d and that will allow us to get our node

05:41.580 --> 05:47.790
ID, which in this case is our game object so we can use it for comparing against other nodes later

05:47.790 --> 05:48.360
on.

05:49.020 --> 05:51.810
Okay, so that's the node.

05:51.810 --> 05:53.010
So save that.

05:53.040 --> 06:00.810
Now we can open up the graph, which is where a lot of the work is going to go on.

06:01.560 --> 06:02.190
Okay.

06:02.190 --> 06:09.390
So the graph will not be a mono behavior either and the initial methods that are in there aren't needed

06:09.390 --> 06:10.050
either.

06:10.350 --> 06:14.580
First, we're going to have each graph a list of edges.

06:14.580 --> 06:23.670
So we want a list edge, let's call it edges equals new list edge, just like that.

06:23.670 --> 06:25.470
Then we want a list.

06:26.070 --> 06:34.470
Node nodes equals new list of nodes.

06:35.790 --> 06:43.660
And then we want a list of nodes as well, which will be called a path list.

06:43.680 --> 06:49.830
Now, this is the list that will get populated by the aStar algorithm, whereas the other two are just

06:49.830 --> 06:52.440
the edges and nodes that exist all of the time.

06:52.590 --> 06:59.250
So this will be a new list of our node class as well.

06:59.910 --> 07:00.450
Okay.

07:00.450 --> 07:08.010
Now we'll have a public graph constructor, which isn't really going to do very much at all.

07:08.040 --> 07:08.710
Just like that.

07:08.730 --> 07:09.000
Okay.

07:09.000 --> 07:18.660
Now two methods will need to put edges and nodes in will be a public void add node and we'll pass through

07:18.660 --> 07:23.370
the game object ID for now.

07:23.400 --> 07:24.300
That'll do.

07:25.500 --> 07:27.120
And then inside of here, we'll go.

07:27.120 --> 07:29.850
Node equals node.

07:29.850 --> 07:33.300
Node equals new node.

07:35.490 --> 07:36.460
I.D..

07:37.770 --> 07:43.470
And then nodes list dot add node.

07:45.340 --> 07:45.700
Okay.

07:45.700 --> 07:47.020
So that's adding our node.

07:47.020 --> 07:56.680
Then down here we'll go public void, add edge and let me put a capital in there for consistency.

07:56.680 --> 08:00.220
And inside of here, an edge gets created between two nodes.

08:00.220 --> 08:08.770
So it'll be game object from node, game object to node.

08:09.400 --> 08:17.890
And then node from node equals find node from.

08:18.600 --> 08:19.440
Node.

08:19.560 --> 08:29.790
So we're basically taking this from Node as a game object and we have to look through our nodes list

08:29.790 --> 08:33.600
to get this node to return it as the node form.

08:33.600 --> 08:38.490
So we still have to write, find node and let me put a capital F for there.

08:38.490 --> 08:49.110
So that's how from though then then we'll go node two, which will equal our find node to node.

08:52.610 --> 08:52.970
Okay.

08:52.970 --> 08:53.980
We'll come back to this.

08:53.990 --> 08:57.530
We're going to put in this find node method.

08:57.530 --> 09:08.480
So node find node game object or ID and we'll loop through our list and look for it.

09:08.480 --> 09:13.370
So for each node n in nodes.

09:14.690 --> 09:30.440
If n dot get ID equals ID, then we will return n, otherwise we will return null.

09:31.850 --> 09:39.560
So that's just looping through those, finding the particular node that we're after, using the ID of

09:39.560 --> 09:42.040
the node, which is in fact the game object.

09:42.050 --> 09:44.210
Then back up in the add edge.

09:44.210 --> 09:51.590
We'll go if from does not equal null and two does not equal null.

09:51.590 --> 09:55.160
So we're checking that these two nodes actually exist.

09:55.280 --> 09:56.520
Then we can go.

09:56.540 --> 10:11.390
Edge E equals new edge from two and then edges dot add that edge into our edge list and then we can

10:11.390 --> 10:21.140
go from dot edge list dot add a so remember our edge list is what our node has.

10:21.380 --> 10:26.690
So we're actually adding in the edge that's coming out of our from just down in there.

10:26.840 --> 10:27.440
Okay.

10:27.440 --> 10:31.910
So that's the starters of our graph.

10:32.180 --> 10:32.600
Okay.

10:32.600 --> 10:37.730
Now I've got one little issue that's going on down here because I haven't got a closing bracket for

10:37.760 --> 10:38.060
that.

10:38.060 --> 10:40.250
So let's just put that in there to get rid of it.

10:40.250 --> 10:47.030
And now I can save that and we'll finish up with this lecture because we've done enough.

10:47.030 --> 10:54.410
The next part of this will be putting in the a star algorithm to search through and find the paths from

10:54.410 --> 10:57.110
one given node to another.
