In the previous video we wrote The Idol and the Patrol States.
We now need to create the A.I. class, which we will put on our guard here so that he will be able to
transition through states.
So let's right click and create a C-sharp script called a AI and we'll open that up.
Are you going to be quite surprised just how short this code is?
So this is the code that we will attach to our NPC, which is UG in this case.
First of all, we need to make sure we grab hold of the nav mesh stuff.
So using unity engine AI at the top.
And then we also want to get all the bits and pieces that we need to send through to our states.
So we need our nav mesh agent.
We need the animator.
We will a little later on need the player as well because we need to get your NPC to chase the player.
So let's just put a public.
Transform player in there and we will create our state, which is the current state.
So that's all we need there.
We just have to remember it to give the player to this code in the inspector.
Right in the start we will grab hold of our agents.
NAV mesh agent.
So agent equals this dot get component.
NAV mesh agent.
And the animator, they get component.
Animator.
And then we will create our first state.
So current state will equal new idol.
And for it we need to pass through all the bits and pieces.
So this particular NPC.
So this game object.
Then it needs the agent and the animator and the player.
Now you could do a get component inside of your states and just pass through the game object, except
that we're creating and destroying new states all of the time.
And so it'll end up lagging a little bit.
And I do have a little bit of an admission that later on we will actually get to a get component inside
one of the states.
But there is a way around that by coming back here and making sure that you grab everything right at
the beginning when the A.I. is set up.
Now, in update, if you've created any A.I. before, the update is where all of the magic happens and
the update can get like true spaghetti code.
It can get really, really messy and very hard to debug and hard to get right.
But using these states in here we put current state is going to equal the current state dot process.
So remember process in the states is working through the enter the update and then the exit and then
when it gets to exit, it returns the next state that it needs to go to, not even exit.
If it gets to somewhere in the update that it needs to throw out, it sets the new next state, which
then gets passed back in the exit.
Let's just remind ourselves.
So go back into state and let's just go up the top where all that is happening here in process.
So it's probably much easier to explain if we're actually looking at it.
So the process is going to run from the AI that's asking it to.
And then if stage is enter, it's going to run the enter.
The enter is then going to make sure that stage gets updated to update.
So the next time that the AI update calls process, it's going to then run this update and it's going
to keep doing that until the update itself flicks you across into the exit state, at which point the
exit gets run and then it returns, whatever that new state is.
And if we have a look down in idle, we can see that happening right here where new state is getting
created to be patrol.
So with a 10% chance in idle, we will go into a patrol.
All right, so that's your AI class.
That's it.
That's all we need in there.
And you know, you can add a few other things if you like, but as far as the behavior is concerned,
that is all we need.
And we just keep adding more states and linking them up.
Okay, so we'll save that.
Now go back to Unity.
You want to drag and drop that II code on to ARG.
So find ARG in your hierarchy, drag and drop that AI onto there.
Now we've got an PHP controller that will have come with your project so that you can walk around,
get that and drag that into the player transform part that's exposed.
Again, it doesn't come into play, but it will a little later on.
All right.
So now we're ready to press play and have a look.
So let me just make this game screen a little bit bigger.
Press play.
All right.
So you can use the WASD keys to move around all the arrow keys for this player controller and we can
walk right up to ARG.
He's not programmed to attack us yet.
Now he is with 10% chance meant to go on a patrol, but he doesn't appear to be.
Now I know why this is, and I'll go back and show you that in a moment.
But for now you can, I guess, just enjoy his idle state.
And that's what he does look like when he's in idle.
Okay, so just press escape to get your mouse cursor back and stop playing and we're just going to go
back into the state code.
Let me show you where that issue is occurring.
Okay.
So the really, really important part of this whole state system is that this here works nicely to go
from one thing to another.
So we go into we go update, update, update, update for as long as you need to go update and then
we go into exit and then return the next state that we need to go to.
What's happening in idle and I've probably done this wrong in patrol as well.
If we go down to idle, let's have a look.
The enter runs at the moment is setting is idle and then it's running.
Its base is enter.
Now its base enter is setting event update, which is great because next time process comes around,
it will run the update.
Now let's go back down into the update.
So now we're coming to update with a random chance.
We start patrolling.
Then we set the stage event to exit, which is all really good because that's going to progress us to
the next stage within our process.
But does it?
Because look down here, we're running this based update and if we go up and have a look what it does,
it is going ahead and setting our stage back to update.
So even though we've set it to exit, it's just going to run back to update.
So we actually don't need to call it in here.
And then doing it in here is just wrecking our code so you could get rid of it because it's not doing
much for us.
Or you could put it above this if statement and allow the if statement to then change it.
So let's just get rid of that out of there.
Now I will just check that I didn't do that in patrol as well because that will give us the same little
situation later on because we're not actually switching out of the patrol.
Once we go into patrol, we're stuck in there forever at the moment.
Let's just get rid of this update because it's kind of superfluous setting update to update constantly.
Okay.
So now that we're confident or I'm confident that this is going to work, let's save it.
We'll go back into Unity Press play and then let's see.
So you can see he's immediately gone off on patrol.
So 10% chance.
That's checking for that.
10% chance.
Every single update.
So that's quite quick.
And now he's going to walk between all of the waypoints.
Now he's not picking them up in order because it will depend on how the game environment goes ahead
and updates those waypoints.
He will, however, go through one by one.
So in this case, the order that that get game objects with tag code that we've got in the game environment
is getting them in a cross like pattern.
Sometimes it does get them in a square pattern, but this time it's not.
So he's just going to patrol and go between all of those points forever and ever.
All right.
Now, you're probably thinking at this point, how can I get my waypoints in the right order?
Well, one way you can do it is position them in order according to their name.
So if we have a look in the hierarchy, we've got these checkpoints like this and they're situated so
that they go around the actual map.
If you want to pick them up in alphabetical order, then you could use their name and use a system link
in there instead.
So if you go into your game environment's code where you're actually picking up those checkpoints to
do that, you just have to add two lines of code, which I've already done here.
The first one at the top is our library.
So add in system dot link that will allow you to do a little order by on your list.
And you can see that down here after we create the instance.
So instant check points is going to equal instant checkpoints ordered by the waypoints name.
And then we're just setting it back to list and putting it back into our checkpoints.
So this will reorder them into alphabetical order, ascending alphabetical order of the name.
So you could go through and rename them like waypoint one waypoint to 8.3, whatever you want, as long
as they're alphabetical and then have that particular order.
It's because we're doing a find game objects with TAG that we can't ever be assured of the actual order
that we will get those game objects in, but we can then force an order using that.