In this lecture, we will look at the concept of fleeing.
It's a common technique used in AI for getting an NPC to move away from a location.
The technique can be used on any type of NPC.
In this case, we will apply it to our crowd.
We've been working on mathematically fleeing.
Works like this.
An object or location is introduced into an environment.
The NPC determines where the object is in relation to their own position.
They then calculate a new goal location that is away from the object.
In this case, we will use the exact opposite direction.
This can be calculated using the vector towards the object and taking that away from the NPC's position
with the new goal location.
The agent can turn around and start heading for it, thus being taken away from the object in question.
Other factors we can consider are the radius of influence of the object and the cooldown period.
The radius determines how close an NPC must be to an object to be affected by it, and the cooldown
is how long the NPC flees for before returning to normal behavior.
In the previous lectures we looked at creating our crowd.
What we're going to do now is actually get them to flee from a potentially dangerous object.
So I'm just going to turn a few things off that we've been using and turn some things on.
So first of all, you're going to turn off the buildings and the shop, so I'm just going to take them
out by unpicking them for enabled an inspector, the main camera.
I will put that back on and my F P's controller, I will turn that one off.
Now once you've done that, you'll want to come over to your navigation mesh.
And in the scene view we're going to do a re bake because we want people to be able to walk everywhere.
All right.
So we've now got to find our cubes, which were our goals.
Okay, so here's one here and here's one here, and there's another one just down here.
Bring them up so that together.
Now you can move them or you can create some more.
So I'm just going to duplicate that one up in the corner and put one over there and then over here and
over there, we just want to get the crowd sort of moving around this square.
Okay.
We're now going to add in our undesirable object that we want to flee from.
Right?
So I'm just going to roll up a few of these things.
In fact, I might grab some of these people and actually make them children of this city.
BLOCK objects.
I can actually roll them up and just get rid of all of this extra mess that's happening inside of the
hierarchy, which will allow you to see it a bit clearer.
Okay.
So let's put this girl up into the city block as well.
Okay.
So let's now right click in there.
And 3D object.
We're going to add in a cylinder.
Now, the cylinder we want to move up so that we can see it.
So hit the W key.
Let's bring it up above the ground.
Make sure that e's above the ground and then we're going to color that green.
So I'm going to create a new material.
Call it green, make it green.
So select it.
And then in the inspector, set the color for it, the albedo color to green, put that onto the cylinder.
Now with that cylinder, it's going to get dropped when we click on the ground.
So basically we don't want it there to start with and we're going to get it and then just put it into
the assets folder, which will create a prefab of that cylinder for us.
Then we can go ahead and delete it out of the hierarchy.
Now we need to add in some code to drop the cylinder in place.
So down in your assets, right click Create C-sharp script, let's call it drop cylinder and we're going
to double click on that and edit it.
Okay.
So to begin with, we need to get the prefab for the object that we're dropping.
So it'll be a public game object.
Obstacle.
And then we want a game object array of all the agents that are in the scene so that we can tell them
the obstacle has been dropped in our start.
We'll go agents equals game object dot find game objects with tag.
So we have to tag all of our agents with the agent tag, which we'll do a little later on.
And in the update we will test if we've clicked on the ground and if so, we will instantiate a green
cylinder.
So if input dot get mouse.
Button down.
Is the left mouse button.
Then we're going to do a ray cast hit hitch info.
Right.
Is going to equal the camera dot main screen point to ray input dot mouse position.
So this line projects a ray from the click position into the environment and we're looking to hit something.
So we do an IFF physics.
Dot Ray.
Cast Ray.
Origin.
Ray.
Direction.
Out.
Hit info.
Then we want to instantiate.
So if we have hit something, we're going to instantiate our obstacle.
At the hit info point
obstacle transform.
Dot rotation, which is just going to be the default rotation of the obstacle because INSTANTIATE needs
a rotation, so we'll just put one in there for it and then we're going to send the message to each
agent that we've actually dropped the cylinder.
So this will be a for each game object A in agents.
And for now we don't actually have any code that's going to do anything with that.
So we'll just leave it empty.
And so it doesn't actually give you any errors.
Let's just comment it out for now, but leave it there.
So save that.
Now we're going to go back into unity.
And I want you to drag and drop the drop cylinder script on to your camera.
I mean, it can be on anything in the scene just as long as it's in there somewhere.
So on my camera I've got drop cylinder down here.
The obstacle has got cylinder in it.
You need to drag and drop this prefab cylinder into that spot in order to get the cylinder to instantiate.
The other thing is that this script that we just created uses the main camera and it won't be able to
find the main camera unless the camera is tagged with main camera.
So when it comes through initially this camera, I think it's going to have game controller on the tag
that you've got.
Make sure you set it to main controller.
Okay.
So now let's press play and we'll click on the surface here and we should get some cylinders.
Now I'm getting an error about a tag not being defined for age.
That's a fine.
Just ignore that because we'll come back and put that in in a moment.
Right.
Okay.
So we now need to work out how to get these agents to flee and we'll come back and work on that in the
next lecture.