Okay.
How are you coping with Easter so far?
Well, we haven't even got into the code yet.
And there's an awful lot of code that we have to write.
But hopefully it's going to seem quite familiar because we've gone through all of the basics and how
it works.
So let's begin now inside of this class.
First of all, we need to grab hold of our actual maze so that we can get its map.
So public maze, maze.
Now, this is going to be a little bit of a combination between actually running the algorithm and then
also showing it, visualising it on the screen.
So we're going to have some constructs that are just for all the number crunching and everything and
then others that are going to show things up for you.
Okay.
So first of all, I'm just going to add in two materials that we'll use to change our nodes from one
color to the other as they're processed.
So material, closed material.
And you can probably guess what the next one is, public material, open material.
Then we need to create our two open and closed lists.
So we're going to have a list of path markers.
Four Open equals new list path marker.
And then we also need one for clothes.
So let's just copy that.
And make this closed.
And then we need to bring in our game objects that are going to be our actual physical markers on the
map that we can see.
So public game object.
Start.
Public game object and.
And public.
Game.
Object.
Path.
Point or path marker or whatever it is.
Okay.
I'm just going to call it that.
So let's just save this and we will switch back into unity.
And we want to go to the Maze Game object in the hierarchy and drag and drop find path a star script
onto there.
Now we're going to populate these items in the script.
So the maze is the script that is generating the maze, which is this recursive script.
So just drag that down into there.
We then have a start and end and our path p markers as well as a closed and open material.
Now attached to this lecture there is a little unity package with some materials and prefabs in it.
If you bring that in, you'll find you get a models folder here which has materials and prefabs.
So if we go to our prefabs first and then select our maze, we can put the start object into the start,
the end into the end, and then the path marker into the path.
P Now we can also set our materials for the closed and open nodes as we're going through.
So for closed I'm going to use red and for open I'll use green.
So this will just make the whole thing more visual so that you can see exactly what's going on as the
star algorithm starts to work.
Okay, so now let's go back into our code.
Now, these three things are our game objects that we're going to instantiate and then hold onto them
inside path marker objects.
So we'll create a path marker for the gold node and we'll also create one for the start.
So path marker, start.
Node.
We'll also need another path marker.
And this path, mark, is going to keep track of the last point that we're at, so last position in
this case.
And then we're also going to keep track of a bull called Done, which will be true when we've found
our way to the end of the path.
So let's initially set this to false like that.
Okay?
So that's all of the things that we need at the top, all of our variables.
All right.
So I'm going to save that again, switch back to Unity and show you something before we continue.
So if we go into our prefabs where I've got these markers and just click on one, if you go over into
the inspector, you'll see that they are tagged as marker.
I've done this so that we can remove them with a key press out of the map, which will come in useful
in a moment.
So what we're going to be doing is having the ability to be able to remove them all and then do the
ASR over and over without restarting our code, which means you can have a bit of a play around with
it and get used to the results.
So let's create a void, remove all markers, and this is just going to be a really quick loop that
just destroys everything.
So game object.
A ray of Marcus.
Equals game objects.
Find game objects with.
Tag.
Make sure you use game objects with the SE and that'll be marker.
Okay.
And then we'll just do a quick then we'll just do a quick for each game object m marker in markers.
Destroy em.
So that will be called shortly.
All right, let's now begin our search.
So void.
Begin, search.
We will allow this to be called over and over again, as I said before.
So every time we do, we're going to set done to equal false.
And then we will run.
Remove all markers.
Okay.
So now we can actually go about creating our goals and everything.
So first of all, we have to pick two positions in the actual maze that are going to be our start and
end.
And we could do it quite randomly, where we just keep picking random values and seeing whether they
are a space or a wall and of course, use the space.
A better way is to actually put all of the spaces or all of the maze locations that aren't a wall into
a little list.
Shuffle that list using the shuffle method that you'll find in the extensions file and then just like
pick them off the top.
So I'm going to create a list of map locations, and that's going to be called Locations Equals New
List.
Map location.
Okay.
We're then going to create our nested loops to loop around our map.
So if we want to for int z equals one to Z is less than our maze depth.
Minus one and then Z plus.
Plus.
Same thing for.
X.
So let's change that to X and X.
And in this case, we want our width and make that x.
And inside of here we're going to test if may stop map for that x.
Z does not equal one, then we will put it into our list.
Locations dot add new map location.
X.
Z.
So why are we using not equals to one in this code?
Well, in the mice code, if you have a look in there and the recursive algorithm that's writing it
in the mazes that we're generating, there's a value of zero where there's an empty space and a one
where there's a wall.
However, when you're creating mazes, you might have other values that represent other things inside
of that maze area.
So we're only really worried if there is a wall there at the moment as far as where we can move.
And that's why we're using not equals to one here.
So we do know that ones are the walls and the starting positions can be any other value at this point.
So that's why I've got not equal to one.
So but the time we've gone through this, we now have all of these locations.
And so if we just shuffle them, so locations shuffle.
That's going to give us a couple of values sitting on the top of that list that we can go ahead and
pluck out and use as the goal location and also as the starting location.
So let's go ahead and do that.
So after the shuffle, I'm going to create a vector three, which will be our start location.
And we'll set that to a new vector three and to get the values, we're going to pluck them out, as
I said, the location and we're going to give it locations at position zero dot X and then we can just
put it zero for the Y value and locations, zero for the Z position.
Now we're putting zero as the height because I have done that with pretty much everything that we're
just zeroing it as a flat surface.
So it's basically sort of two D at least.
And then once we have this location, we're going to instantiate our first node at that location.
So I'm going to create a start node.
And that's going to equal a new path marker.
Which will be a new map.
Location, locations zero dot X, locations zero dot Z.
So the same uses you're using for the position.
And then we're going to send through a zero, then we're going to zero our G, our H and our F values.
And for the game marker, I've just come down a line so I don't go off the screen for you.
We will instantiate the start.
Start.
Location.
And then we'll just put Kitten in dot identity for the rotation because that's not really important.
And finally, the parent is going to be null.
So we'll just put that there.
And for the goal location, we do the same thing.
So I'm just going to copy all of that, save typing it out again, put it down here, make sure that
we're talking about the goal location, the position of the goal location.
We're going to use that as the second value in that locations array that we shuffled.
So let's just set all of those values there to one.
And then this node down here is going to be our goal node.
Now, at the same time that we actually create the path marker, we're going to instantiate those prefabs.
So this one here is actually going to be the end prefab that we sent through before.
Okay?
So now we can actually run, begin, search and we'll be able to see those markers come up.
So I'm going to do that down in the update.
So just in here, let's put if input dot get key down is key code dot p.
Then we'll do a bargain search.
Now, one little tweak we need to make to this code is to add in a scaling value because the maze itself
uses a scale to make it bigger on the screen for you to actually bring it out and the actual positions
in the map there.
Okay, they don't need to be scaled up.
They're still in light unit positions.
But our start location and our goal location need to be scaled up according to what the map scale is
set to.
So for the actual map values, we're not scaling those up by the map scale, but for any positioning
we do of objects.
So what I'm going to do up here is put in multiply.
Maize dot scale.
And then we want to use that value there and put it for our Z location and also for our goal location
here.
Okay.
Right.
So that's now going to give us a better location.
I've also just noticed that should be goal location there as well, so that we can see the goal in the
goal location.
Okay.
So let's save that.
We'll switch back to unity now.
We're going to have a look at the maze for a second before we press play.
And in this case, you can see that my recursive script has the map size at 30 by 30.
You could leave it this big, but it'll be a long time finding a path through there.
So what I recommend is that you actually set it down to ten and ten, so it makes it a manageable sized
maze.
Okay, so now if we hit play, we'll be able to see our small maze pop up.
There it is over there so you can go and focus in on it.
And then if we hit the P key, make sure you've clicked in the game window so that will become active.
Hit the P key and you will see that you get your starter positions popping up there because remember
we're setting them randomly and there's nothing else going on between just creating them.
Okay.
So you can see very clearly where you've got to start and where you are going to end.
Okay.
I'm going to leave this video here for now.
When we come back, we will continue working on the aStar algorithm to do some pathfinding.