In the previous lecture, we set up our base state class.
We're now going to inherit from that and create an idle and a patrolling behavior for our character,
right?
So right underneath that base class state, we're going to create a public class called Idol and it
is going to inherit from state.
Like that.
Okay.
So the first thing it's going to need is its constructor.
So we want public Idol and then it's going to have all of the stuff that our state has.
So let's just copy all of that because there's no point us typing it all out again and just put that
into there.
And then we also after that, let's just go and put in the base and run through the NPC, the agent.
The annum.
And the player.
Details just to pass them through since the base is actually going to store that for us anyway.
And then all we really need to do inside of this constructor is to give our state a name.
So name equals state dot.
Idle so it knows what it is.
Right.
The next thing we're going to put in here, it's just scroll down a little bit is now our enter.
So public override, void, enter and then inside of the enter before want to put the base enter in
there automatically for you if you're using Visual Studio, which is okay because you want to run this
anyway.
And if we have a look back up here what our base enter is doing, it's setting the stage to update.
So that means that we don't actually have to do it down in here.
But what we need to do when we enter the Idol state is trigger the idol animation.
So that's going to be anim dot set trigger.
Is Otto.
And if you have a look in the animator, you'll see that there is a trigger in there called is Otto,
which takes you from any of the other states that aren't idle back to idle.
Right.
So that's our enter.
Now, of course, we need a public override void for update.
And inside of update again, we're going to run the base update as the last thing.
But we also want to do in here is transition to our next state.
So remember I said earlier on in the last video, is that somewhere inside the update, it has to break
itself out of there.
So this is the only place that's going to do it because update is essentially like a little mini game
loop that's going to run over and over again for your state and look after the processing of it.
And in order to get from update to exit, you've got to have some kind of condition in here.
So we're going to do a random value.
So if random dot range 0 to 100 is let's say less than ten, so 10% of the time what we're going to
do is go to the next stage.
So just inside some brackets here, we will put next state equals new patrol and that patrol is going
to need the NPC agent, Adam and player.
We haven't written patrol yet, but it's going to be the new state.
And then in here we actually go stage equals event exit and what that's going to do next time the process
which is up here comes around is it's going to throw you into the exit stage instead, which will then
return the next state that we just set up down here.
Okay.
And then finally we have to put in our public override void and that will be for exit.
And in here we have to keep our base and we're going to put in Adam Dot reset trigger.
Is.
Idle.
Now, this is just a little bit of a trick to do with the McAdam system if you've triggered something
up here.
What it does is it kind of like puts an event into the system which doesn't get used up until the animation
that it's meant to trigger runs.
If that animation doesn't run or doesn't need to be triggered, then that little event still sits there.
And what it can do is cause a few problems later on when you go to set trigger to walking and then your
character doesn't walk is because there might have been an old is idle still sitting there in the queue.
So by resetting it in exit it means any that we threw in there which didn't need to be, is just going
to get taken out and it just going to clean up that animation for us.
And this is what I mean by containing all of the necessary behaviors just inside the one state.
So this is stuff pertaining to idle and how you get out of idle, and that's it.
We don't want anything else.
We don't want to check any other states that relate to other things and and stuff like that.
It's just about the idle.
And now we're going to create the patrol because idle calls on patrol and then we can actually get something
running that you can see.
So underneath the idle state in the states, dot KSFO will add patrol.
So it'll be a public class patrol which is going to inherit from state.
The State needs to move your character around the different waypoints.
So we need an index to count through those waypoints which are in our array that we've set up or at
least we've set up in the game environment.
So let's start with an int for current index and let's make that minus one.
And let's create our constructor.
So public patrol.
Now, this is going to have all of that same stuff that we put for Idle.
So let's just grab all this to save us, typing it out again.
Copy it, and let's just put it in here.
Okay.
Then inside of this, what we're going to do is set the name to state.
Dot patrol.
Agent speed to to and agent dot is stopped equal to.
False.
Now you can stop an agent and have an agent moving around on a particular path.
If you change, this is stopped value.
So if the agent is currently doing something else, we actually want to stop it at this point because
we're going to give it another, I guess, destination to follow.
The other thing you'll notice in here is this Agent Speed, which is another nav mesh agent property,
and that's how fast your character is going to move.
Now, in this case, when I've got UG walking around or, you know, doing the walk animation, then
a speed of two when he moves seems to be not too bad as far as making sure that he doesn't look like
he's sliding around on the ground.
So that's something else that you can consider working with and coordinating.
We haven't added an agent speed up into idle.
Obviously in the idle state the agent is going to be stationary, but because the agent doesn't have
a path that it's moving along on your nav mesh, it will be stationary by default anyway.
The speed of the agent in this case only comes into play.
If the agent needs to move along a path, it doesn't mean if you set it to two, the agent will just
start moving at speed of two.
It's only if it has a path, right?
So that's all you need inside of your patrol.
Okay, so let's go down and we will now put in our enter our update and our exit.
So it's going to be a public override, void enter.
And let's just copy this and we'll have three of them.
So we want one for update.
Make this update.
Like this.
Update and then make this one the exit.
And exit.
All right, so in the enter, what shall we do?
Well, we want to grab hold of our list of our waypoints, and we will actually come back and do a bit
more work in here later on.
We don't need to save our waypoints into anything because remember the game environment, Singleton
has those.
So for now, we'll just leave that out of the enter.
But what we don't want to do is put and set trigger and set is walking to make that animation start
so that your agent will be obviously walking now to move around those waypoints.
We'll do it inside of our update.
And well, first of all, check that the agent.
Hasn't finished its current walk between one waypoint and the other.
So if agent remaining distance is less than one.
Then we're going to move to the next waypoint.
So if current index is greater than the number of waypoints, so game environment, this is where our
singleton comes in.
Dot singleton dot check points, dot count.
Okay.
I've just realized after I've gone a lot further in this video, is that this line is going to give
you an error.
Before you go any further, make sure that your current index is greater than or equal to the check
point count minus one.
So that's the line that you should be updating and keep it like that.
Otherwise, the rest of the code is actually going to count outside of the range of the number of check
points that you've got.
So you won't see this line like this in the rest of the video, but make sure you keep yours like this
to avoid that error happening, then current index is going to be equal to zero.
So we're going to reach the end of the list of waypoints and switch back around to zero.
Otherwise, we just want to go to the next one.
So current index equals current index plus one or current index plus plus like that.
And then once we have that, we can set the destination.
So we'll go agent dot set destination and that destination will be that particular waypoint.
So it will be game environment, dot singleton dot check points.
At the index of current index.
Now the set destination actually needs a vector three, so it will be the transform dot position of
that particular waypoint.
Okay.
Got all that?
And then we just have to end that bracket limit, make this just a bit bigger so you can see all of
that code and you should be getting a really good feel of the singletons at this point is how easy that
after we set up that list of check points, we can actually come back and just grab them in here without
having passed them all through.
So this is going to count through all of our waypoints and then move the agent on one after the other.
Now, current index, if you remember rightly at the top here, is set to minus one.
We actually don't want to try and start moving from minus one because there is no minus one for current
index.
So in the enter we'll also add in current index equals zero so that we start at the first one.
Okay.
So that's the update.
Now, what goes into the exit?
Well, not much really.
All we want to do is make sure that we reset the trigger for the walking like we did with our idol.
So Adam Dot reset trigger.
Is.
Walking.
And that's pretty much it.
Now, this patrol does not reference.
Any other states.
Okay.
And the other one, the idle references to patrol.
So as far as we're concerned, at this point in time, this is kind of enough to get us going to use
the patrol.
Okay.
So when we come back in the next video, we'll write the eye class, which will drive all of these states.