In the previous lecture, we started with the code for fleeing, so we were able to drop a cylinder
into the scene.
What we're going to do now is continue with the code for that and write the code that is going to go
inside of the A.I. control for our characters.
First of all, I want you to find where you put all of your characters.
That's if you rolled them up like I did.
Now mine are under the city block.
We need to select all of the characters.
So shifts select and then make sure that we tag them with agent.
So put a plus in there and agent because the code in the drop cylinder actually needs to grab hold of
all of our characters so that they can send a command to them basically.
So with that done again, make sure just select all of these and make sure I add that tag rather than
just create it.
Okay, so they're all there now we're going to go back into our AI controller.
The first thing we want to do is create a reset method that we can run every time an agent gets to a
goal location.
So void reset agent and we're going to move some of the start code down into here.
So the things that we want are our speed multiplier, which will be this line here, but we're going
to separate it out by putting that float up at the top.
So float speed molt and then we'll grab this line of code and actually these two lines of code as well.
So just get those out of there and put them down into here and we'll set this to speed malt and we'll
set this here to speed.
Molt.
And of course, we need to pass that through to their as well.
Multiply it with the speed for our agent.
Okay.
So besides those things we're also going to set our triggers for is walking and we're going to grab
that one and put that down in here.
That's delete it out of there.
And finally on Reset, we will go Agent Dot reset path.
Now, this is a command that belongs to the nav mesh agents, which allows you to sort of reset the
path, basically, which is to stop them going along their current path and sort of like clear wherever
they're going out of their system.
Okay.
So now that we've got that, we can put that into our start.
So just under here, we will run reset agent like that.
Okay, now let's move down and put in our new method.
That drop cylinder is going to call when a cylinder is dropped.
Right.
So under here, we will put in this public void, detect new obstacle and send in a vector three for
the position of that obstacle.
And then inside of here we'll go if vector three dot distance between the agent so position this dot
transform.
Dot position is less than our detection radius.
Now, that's something we have to define at the top of the code, which we'll do in a moment.
So if we're within the detection radius, then we're going to actually flee from the position.
So vector three.
Flee direction equals this dot.
Transform top position minus position that's being passed through and we're going to normalize that,
which will set it to a length of one so normalized.
And then vector three new goal.
Equals this dot transform position plus fli direction multiplied by fli radius.
Now that's how far we want to actually flee away from the obstacle that we've detected.
So let's create these two objects at the top of our code.
So we want a float for detection radius which will equal, let's say, five, and a float for FLI radius,
which will equal ten.
Okay, so let's continue writing this code down here.
So we've created a new goal location to go to, which is from the position of our player or our agent,
plus the fli direction multiplied by a certain radius to push it out.
So nav mesh path equals new nav mesh path.
Agent dot calculate path.
New goal.
Return the path.
And then we're going to say if path dot status does not equal nav mesh path status dot path in valid,
then we'll actually set the character along that path.
Right.
So just going back to this code for a minute, this is our nav mesh functionality that's built in.
So we're going to rather than setting the destination for an agent like we've done before down here,
we're going agent set destination.
We're first of all seeing if we can calculate a valid path from where we are to where we want to be,
because that new location might actually be off the nav mesh and might not be possible.
So therefore we don't want to set the agent going there if they can't get to their OC.
So if they can get to there, the path is not invalid for our agent, then we'll go agent set destination
and we'll set it to path dot corners.
Path, dot corners, dot length minus one and then annum.
Dot set trigger is running so this will set our animation to and is running the agent dot speed will
equal ten at this point and the agent dot angular speed, which is how fast it turns around.
We'll set that to 500 now because we're setting that there.
We'll have to set it to a smaller value when we reset it.
Because when you go back to walking between the gold boxes, you don't actually want to turn around
really fast like you're fleeing from them.
So up here will go.
Agent dot angular speed equals 120.
Okay, let me just come back down to this set destination.
So when we get the path through here, what it does is it gets a whole bunch of waypoints and sticks
them into a path list or a array called corners.
And these corners are our basic waypoints from our current position to the last position or the goal
position.
So the goal position in this list is going to be at the end of the list, which is why we put this in
here in the square brackets to index it.
All right.
So let's save that now.
We'll go back into drop cylinder because we need to make sure that we run that detection for each agent
down in our update where we put that down in here.
So this is our for loop going through each agent.
We've tagged all of our agent.
So down here we're going to go a dot get component I control.
Dot detect new obstacle.
And we're going to pass through the hit info point like that.
So this will send through the position of the cylinder to the agents who will then flee from it.
So we'll save that.
Let's go back into unity and we'll try this out.
So we'll press play and everyone is wandering around.
Let's drop in a cylinder.
Okay.
Nobody seems to be very fazed by the cylinder, though.
There are some people running around.
Okay.
So that girl, they ran around and away from the cylinder.
Okay, so let's stop playing this.
What I'd like to do, first of all, is actually move into a better position up here.
So let's just come around into that and then we'll grab our camera and align to view so we can actually
drop our cylinders into a better place.
So align with view of the camera.
Now let's go back into our control and what we're going to do up in here for our detection radius is
just set that to 20 to increase the distance of seeing the cylinder because if you don't see the cylinder,
then you can just sort of keep walking towards it, I guess.
Okay, so we're saving that and we're going to run this again and have a look.
Okay?
So once the crowd's kind of all wandering around, we can drop a cylinder like here.
And you can see they all fled from it over here and they're all fleeing from it and going absolutely
crazy.
Now we need to stop them from running once they're set running currently they go to running and then
they just keep running and running and running.
So this is a challenge for you.
Can you figure out how we need to modify the code?
And let me give you a hint.
You only have to insert a new line of code in the update method to fix this.
So pause video.
Now go to give that a go.
And when we come back, I'll show you how to do it.
All right.
How did you go with that?
Well, if we go back into our AI controller, you'll remember that we put this reset agent in here.
We actually need to run that whenever we reach a goal location.
So the goal location is being checked down here in the update.
So what we want to do after we've reached our location before we get a new goal location is to actually
run reset agent, which will set the animations back to walk and also set the speeds and everything
back to normal.
So sort of calm walking around rather than fleeing.
Okay, so if we just save that quickly, go back into our scene.
Now, what I'm going to do is just to test this and it's going to grab all of these people and control
d them to make a whole double set of everybody.
So we'll get a lot of people running around.
Let's now press play.
All right.
So they're all going a little bit too fast to really notice the effect here.
And they're actually going super fast to begin with.
They're not running away.
They're actually running this code here for our speed multiplier, which has got this to value in here.
So I'm actually going to slow these guys down so that we can really see the effect.
And let's put this at 1.5 F in there.
Okay, so we'll save that.
Now, before I play this, I'm also going to grab hold of one of these goal locations and duplicate
it and just put some out here so that we get more people running around into the center of the scene.
Okay, so let's press play and we'll have a look at them as they come out into the center here.
And once they get moving around, we can actually start to drop some cylinders in amongst a whole bunch
of them.
So like down here see them all fleeing away from it.
So that's fleeing.
And I hope you enjoyed playing around with this crowd and this technique.