All right.
So now down into the update.
Let's get rid of the base.
You're not going to need that again.
First of all, we need to calculate the direction to the player and the angle because we want to rotate
the NPC.
So they're shooting in the direction of the player.
Otherwise it's going to look a little bit strange.
So this is going to be a vector three.
We want direction equals player position minus NPC transform position.
Then we're going to get our float angle equals vector.
Three dot angles is the same code that we've written before for finding the angle, direction and NPC
transform dot forward.
And once we have that direction, what we're going to do with it is set the why of it to be zero direction
y equals zero.
Now, why am I doing that?
Because we want to rotate the character around, but we don't want the character to tilt in the X or
the Z axes whatsoever.
We only want it to rotate on the spot which is around its up vector.
Therefore, we have to make sure that any difference between the player and the NPC itself is zero as
far as the height is concerned.
Now to do the rotation, we'll use a return in.
So NPC transform dot rotation equals turn in dot slip.
So we're going to do spherical looping between two angles.
So NPC's transform dot rotation, it's current rotation.
And then we put in a return in.
Not look rotation in the direction we have just calculated, which is the vector towards the player
and we want to rotate that using our speed.
So that'll be timed at Delta time multiplied by rotation speed.
Okay.
So that's going to rotate your NPC around to always be facing the player as it's attacking.
Now, remember, we need a way to get out of update.
So when are we going to stop attacking the player?
Well, if we can't see the player or actually if we can't attack the player anymore, so the player
has gone out of range, then we will next.
State.
Equals New Idol and.
Agent.
Adam player.
You could go back to any state from here, so you could go back to the pursue state or you could go
back to the patrol state.
It's up to you how you want this to work.
The reason I'm doing this is that if you've been in idle, idle actually will let you get to any other
state.
If you wait long enough and you don't have to wait long enough, because what will happen in this case
is if you get out of range, he's going to stop shooting at you, which will flick it back into the
idle state.
Then there'll be like a 10% chance you're going to go into the patrol state and then it's the patrol
state that will determine if it can see the player and it can then walk towards the player and then
attack the player.
So it kind of going in a full loop to come back again.
And it does mean if you run fast enough and get out of range of the NPC, then it's just going to forget
about you and it'll go to idle anyway and then go back into patrol.
Okay, so that's our get out of the update and we just need to go stage equals event exit to make sure
that that happens.
And then finish off with that bracket.
So then we make our way down into the exit and we will have some usual stuff like we've done before
with Adam.
Reset.
Trigger four is shooting.
And then shoot.
Don't stop.
So that's pretty much the code for the attacking and also for the pursuing.
What we don't yet have is a way to trigger those states from starting.
So let's go right back up the top to our idol and we're going to go into the update and set it so that
if the player becomes visible that we start to pursue them.
Okay, so let's just go down in here and let's do it.
In fact, I'll just do it above.
We'll make this one an else if for that.
And then we'll go above here and put if.
You can see Playa.
Next state equals new pursue.
And P.S..
Agent Adam and player.
And then stage.
Equals event dot.
Exit.
So we go straight into the pursue and we can also in the patrol use this exact same if statement.
So if we are patrolling and we see the player will stop and start pursuing.
So copy that and let's go down into patrol and we're going to put that inside of the update.
Let's just put it after here.
As long as it's in the update and it's going to get executed at some point and then it switches to the
new state, which requires these two lines.
Of course, we'll be fine.
Now we know that if you're already in pursue and you get close enough, then it will trigger the attack.
So we're not going to go straight into attack.
We're going to give a pursue.
Now, even if you do suddenly get extremely close to it and it could directly attack you, it will go
via the pursue and not really cost you a lot extra to do it that way anyway.
Right.
So we've got that save that.
Now let's go back and just before we try this out, if you select, I can go to the I code.
We've got this PHP character that we put in there, but this character isn't actually where our actual
character is.
It's sort of holding on to our character.
The character is basically defined by this capsule, so we're more interested in him being able to see
this capsule in his visible range and distance.
So I just get that and change it up in your code.
So it's looking at that instead, right?
So let's press play.
And we might already be in his visual range.
Yes, we are.
So he's just going to start shooting us like that.
Now, if you hold down the shift and se, you should be able to run away far enough.
And then if we can get around these trees, I keep colliding into behind me you go, he'll lose interest
and go back to patrolling.
So we should at this point be able to sneak up behind him.
Okay.
Let's just show you something else in here.
So if I get past him and get in his face again, he will start chasing after me again.
Shift down to run away.
Eventually he's going to lose interest.
And go back.
Now, if we follow him and have a look, what happens is he will actually head straight back to Waypoint
Zero because he is programmed to create a new patrol state every time.
So that's not particularly realistic.
He should, when he finishes chasing us, go back to the nearest waypoint and follow that.
Now, this will inherit some issues that you've got with walking through geometry and that.
But because he's a nav mesh agent and because we have colliders and that and the environment, he should
be able to kind of get around those.
Okay, so let's just stop playing.
Now, what we're going to do instead is just update our code to find the nearest waypoint.
Resist.
Okay, so let's open up our code for the state and we're going to put that in to the patrol state into
the enter, where we will find the closest rather than just using zero.
So let's get rid of this current index and instead we're going to figure it out.
First of all, we need a float to store in the distance that we're calculating between each of the waypoints
and the NPC and to remember the smallest distance.
So we'll start with a float called lastest and we're going to set it to something very big and very
big means infinity.
You can't get much larger than that so that every other distance we compare to begin with is going to
have to be smaller.
Okay, now we need for int i equals zero.
I is less than and here comes our fantastic singleton.
Dot check points don't count.
I plus plus.
So this is our for loop that we're going to go around them all then we're going to get the singleton.
So game object.
This way point equals.
Game environment.
Dot singleton.
Dot check points.
I.
Then we work out the distance.
So float distance equals vector three dot distance between the NPC dot transformed position and this
waypoint dot transformed position.
I'm all writing this out in separate lines.
Of course, if you want to put it in one great big line, that's fine.
But as far as your readability goes, if I did that, you wouldn't be able to see what was going on.
Okay, so we've now got our waypoint.
We've got the distance that we're going to go.
If the distance we just calculated is less than the last dist than this way, a point must be closer.
Therefore, current index is going to equal.
And last distance is going to equal distance.
So we're updating each time.
So every time that we get a smaller distance, last distance, we'll get smaller and smaller until we've
found the one that we want.
Now, there's one little tweak that I have to do in here to get this to work.
We know that we're getting the index of the waypoint that's closest to us, but where the NPC is going
to head is to the one that's after that.
Now, how do I know that?
Well, down in the update, I'm adding a one to it or setting it back to zero before I set the destination.
Therefore, we actually have to sit this one here to minus one or some way said it inside of the update,
which is going to make it a little bit difficult because the updates are constantly running around.
So it's going to be easier here if we go minus one and then when we come in to update, let current
index update it.
So if the closest one is say three then.
And we don't have the minus one.
It's going to come in here and add one to it and give us four.
And so we're going to head to the wrong one.
So we need that minus one up in there.
Okay.
So just telling you that, so that you don't use this sort of code to find your closest proximity to
a list of things without getting rid of this minus one.
Okay, let's press play and have a look at his behavior now.
So we're going to get shot at immediately because of where we are.
So this is Waypoint zero.
Where is actually standing?
Let's let's go over here.
He should just follow us around.
It's easier if I go forward because I'm bound to run into something.
Okay, so now I can go fast back here until he loses interest.
Okay, so he's walking.
Should turn around shortly if he doesn't catch up with me.
There we go.
Okay, so let's sneak back and follow him to see which waypoint he goes to first.
So he should pick up his closest waypoint, which is that one.
And then when he gets there, he will continue on his route, as you just saw then.
So he doesn't need to go back to the start each time because that makes no sense whatsoever.
Okay.
So that is the state pattern in finite code.
And the AI is extremely short and we're.
Totally relying on these states and the code in these states to embody a single state, but also tell
the NPC how to move from one state into another.
So it's basically once you've gone into another state where you came from is completely forgotten about.
And if you did need a system that was like that, then you may actually want to keep some kind of link
to the previous state, which you could if you go to the top here.
And where do we have our next state declared?
We've got our next state here.
You could have another one that says the last state that you came from.
In a way, it'd be like a double E linked list where you can go back and forth between the states if
that's what you needed.
All right.
So I hope you enjoy learning about the state pattern, because I think it's really, really powerful
and a lot of fun to play around with.
And you can build so many different systems with it.
In fact, the GOP's system that I talk about in my course actually uses a state class system very much
like this one.