Okay.
Thus far we spent a lot of time coding and not very much time being challenged except to follow the
code and get it implemented, of course, but nothing where you've sort of been able to extend your
knowledge.
So this whole video is dedicated to a challenge that I'd like to show you that I've set up.
All right.
So I've got my character.
Let me just move my player character away so he doesn't start shooting immediately because that just
gets annoying.
Okay, so let's press play and I'll show you what this does.
So I have created a cube in the environment which is there.
Now, this cube is a safe location and I've got it tagged with safe as a f e in capital letters.
Now what happens is that ARG is still going to do exactly what it was doing before, except we now can
sneak up behind him and give him a fright.
So if we get within a angle, viewing angle or a negative viewing angle of 30 and a distance of two,
it basically is going boo.
And off he goes.
He'll run over to that cube where he's now safe and then he will come back and continue on his patrol
as long as you're not close enough.
So let's go and see if we can scare him again.
We'll come up this way.
And get into their loop.
Scared him off.
He goes back to the cube.
Now I guess if we stay here and he turns around, he's going to come and shoot at us like that.
Okay.
So that behavior is still there as well.
All right.
So that's basically what I want you to do.
I want you to add in a runaway state to our code and to run away to a safe location, which is this
cube.
So there's the cube.
And as I said before, the tag for it is safe.
Right.
So big challenge.
Pause the video now.
Go to that.
And when we come back, I'll step through it with you.
The first thing you need to do to get this to work is to create your cube or your safe place.
Doesn't matter what it is.
It just needs to be a game object that has a position in it and that position you're going to use as
a set destination for your agent.
Now, it's very important that you tag this with something so that you can identify it.
So over in the inspector, what I did with this particular cube is I went into tags and I created one
called Safe with a Capital S and ticked it to make sure that that is my safe location.
And you can put that anywhere you like in the map.
Now the rest of the functionality all happens within the state file.
So open up this state code file.
And what I've done is first of all at the top with states, our enum, I've added in one called run
away because that's what we're going to essentially be doing running away.
Now going back down here where we're doing a can C player and attacking in between those I've added
in a new method which is going to determine if the player is behind the NPC at a distance of less than
two and a angle of 30.
Or at least that would be an angle of 60 because it's only calculated on a half.
So we get the direction.
Now the direction this time is going to be the other way around.
And if you have a look down here at can attack player, the direction is from the NPC to the player.
This time I'm calculating a different vector that's from the player to the NPC so that when we get the
angle, we're calculating that angle based on the back of the character, not the front.
So the angle itself, the calculation for that is the same as we used before just up here.
And the only difference is that we've got this direction vector facing in the other direction.
Then we check for the magnitude being less than two and the angle being less than 30 and then we're
going to return.
True, it means we are behind the character, otherwise we will return false.
So we're going to use this is player behind.
Now we're going to trigger that inside of the patrol.
So if we go down to where patrol is and have a little look in the update, we'll be able to see where
that's added in here.
So underneath the cansee player we're going to test is the player behind the NPC.
If it is, it will then change the next state to become run away, which we're yet to write and then
exit.
So what does run away look like?
Because it's the one that's doing all that functionality now.
It's right down the bottom of my code and it's really quite simple and short.
So the safe location, I've created a game object at the top.
Now what you might have done, which probably would be slightly better than what I've done, is to put
the safe location into your game environment.
Singleton And set it up in there.
So it's always available.
Now that's going to be a less of a drain on processing than getting that safe location with the find
game object with tag every time you have to run away.
So do that if you like.
But I've done it this way now within the set up for the actual run away in its construction, we name
it, and then we set the safe location game object to find the game object that has safe on it.
Now it could have been a tree.
Even in that environment, it doesn't matter what it is.
We only need a vector three position for our agent to run to.
Now we have our enter our update and our exit and they're quite short really.
Let's have a look very similar to the ones that we've had before.
So the anim is set to running.
I decided that he's going to run away.
Should use the run animation where then going to set agent is stopped to false so that we can create
a path for him.
The speed I've set to six, so it's a little bit faster than he was running towards you before.
And then in the same enter we're doing a set destination for our safe location, dot transform dot position.
Now the reason I've put this in enter rather than inside of update, if you remember I talked about
setting a destination and then the game kind of needing to go through a whole update loop before that
path has been calculated.
Putting it in enter is going to do that for you because it won't be until the next game loop or the
next update that we come back in and do the update that is on the state, which means by then we have
our path calculated and we can then test for the remaining distance.
Now the remaining distance in this case is less than one.
So when the NPC pretty much hits the top of that cube, we're going to go back into the idle state and
remember the idle state then has your 10% chance of going into patrol, which he does quite quickly,
and that's pretty much it.
So now you can see the power of creating these finite state machines and the state class.
And as long as you have your enter your update and your exit code, taking care of the transitions between
states, it makes it very easy just to keep adding more and more behaviors for an NPC.
So it's very powerful concept.