๐Ÿ”นMake Animal flee when see the animal Player


Let's make an example of animal fleeing when they see the Player, in this case lets make the rabbits search for a safe spot when they see a Wolf.

Create Scene and main prefabs

1- Use an Empty Scene and add a Plane with a scale 10,1,10.[Pos and Rot (0,0,0)]

2- Set it to Navigation Static and bake the Navigation Mesh

3- Drag to the Scene the Wolf, FreeLookCameraRig, and the Rabbit PA AI Enemy Prefabs

You can use the search bar on the Project window.

It should look something like this: (Also remove the default camera)

.. and move the rabbit away from the wolf.

Waypoints for the Rabbit

1- Create an Empty gameObject and add the MWaypoint component [Rename it Waypoint (1)]

2- Duplicate it 4 times and distribute it on the Scene:

3- Connect the waypoints together to make a route

That way when the Rabbit arrive to a waypoint it will continue to the next one.

Hide Spot Zone

1- Find the Zone Enter Hole prefab and drag it to the center of the route.

2- Create a new Tag ("HideSpot")

3- Select the Enter Hole Zone and add the component Tags. Add HideSpot to the Tag List

4- On the Next Waypoint on the zone also set the 4 waypoints created on the scene

When the rabbit comes out of the Hole it will continue patrolling by going to any of the waypoints.

Creating the AI States

We need 3 AI states to create the Flee behaviour.

  • First State the Rabbit will Patrol and Keep an Eye for the Player.

  • If we see the Player then will go to the second State which it will search for the Hide Spot (Enter Hole Zone).

  • If we Find the Zone we will go to the Third state, that makes the rabbit go to the Hide Spot and stay there for 10 to 15 seconds.

  • When it finish waiting it will return to the First state.

1- Create the States and give them proper names:

Creating the AI Decisions

We need 3 Decisions:

1- Create a new Look Decision "Look for the Player".

Make sure all the parameters are set as the picture below.

We are looking for the Animal Player which in this case is the Wolf, so the Wolf must be set as Player.

2- Create a new Look Decision "Look for Hide Spot"

In this case we are Looking for all the Gameobjects that have the Tag: Hide Spot, which is the Zone Enter Hole. and If we find that game Object, assign it as the target to go.

3- Create a Wait Decision "Wait 10-15 Seconds".

Setting up the AI States with Decisions and Tasks

1- First AI State (Rabbit 1 Patrol), Task and Decision must be set like this:

2- Second AI State (Rabbit 2 Find Hide Spot), Task and Decision must be set like this:

3- Third and last AI State (Rabbit 3 Hide and Wait), Task and Decision must be set like this:

So if the Rabbit sees the Player Animal then it will find the first safe spot near him and it will go to it... in this case because is a zone it will play the zone animation.. wait 3-6 second and then it will return to the patrol state and the cycle is completed.

Assign the AI State to the Rabbit

Finally Assign the (Rabbit 1 Patrol) Ai State to the Rabbit. and assign the the AI Control the First Waypoint.

and that should be it... if the Rabbit see the player (wolf) he will Hide.

Last updated