> For the complete documentation index, see [llms.txt](https://malbersanimations.gitbook.io/animal-controller/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://malbersanimations.gitbook.io/animal-controller/how-to-ac/make-animal-flee-when-see-the-animal-player.md).

# Make an Animal Flee When It Sees the Player

## Overview

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

{% embed url="<https://streamable.com/rrdht1>" %}

## 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)]

![](/files/-M9KQjW_mmH91Vg9B-5u)

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

![](/files/-M9KfETU_DCQC0JXgJl8)

![](/files/-M9KfWsATjpKh9zEjm52)

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

{% hint style="info" %}
You can use the **search bar** on the Project window.
{% endhint %}

![](/files/-M9KSEBFIZr8tJ1KNWZI)

![](/files/-M9KSLupp-EsrrCE13ld)

![](/files/-M9KSStHuJEzXJyDV16X)

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

![](/files/-M9KSwaeiuhv179T7RKp)

.. 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)]

![](/files/-M9KUFbgTKjPb7fRH2Gf)

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

![](/files/-M9KUnLl5WKVpXqf3B4L)

3- Connect the waypoints together to make a route

![](/files/-M9KV7v8WLGqVYS6-9Ej)

That way, when the Rabbit arrives at a waypoint, it will continue to the next one.

![](/files/-M9KVFRY9OkXdvkBnLFB)

## Hide Spot Zone

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

![](/files/-M9KW7P365_I9pTEHem7)

2- Create a new **Tag** ("**HideSpot**")

![](/files/-M9Kgf4w_nGh2L4nzyVc)

![Rename the New create Tag to "HideSpot"](/files/-M9KYhC2UbS9afhcQS_q)

3- Select the **Enter Hole Zone** and add the component [**Tags**](/animal-controller/scriptable-architecture/scriptables/tags.md). Add **HideSpot** to the Tag List

![](/files/-M9KYxwTTH-NlADOZ1r0)

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

![](/files/-M9KZycJwF7qpUwo5Mju)

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 it sees the Player, it will go to the second State, where it searches for the Hide Spot (Enter Hole Zone).
* If it finds the Zone, it will go to the third State, which makes the rabbit go to the Hide Spot and stay there for 10 to 15 seconds.
* When it finishes waiting, it will return to the first State.

1- Create the States and give them proper names:

![](/files/-M9KbtCgJtW7ZmzBOEDU)

![](/files/-M9KbbLi0ELeUUo7ApmX)

## Creating the AI Decisions

We need 3 Decisions:

![](/files/-M9KfzqaRz7qp_CqAPMD)

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

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

![](/files/-M9KrB2QnMYjQjFp-NsB)

{% hint style="info" %}
We are looking for the Animal Player which in this case is the Wolf, so the Wolf must be set as Player.
{% endhint %}

![](/files/-M9Ken1YEJC2KIINTBSO)

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

![](/files/-M9Khl8y44nKvUW2sCLi)

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".

![](/files/-M9KsjXvqSXxnlk_jt1i)

## Setting up the AI States with Decisions and Tasks

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

![](/files/-M9KsBgxn6bVKk_E1JT2)

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

![](/files/-M9KqIK2r11r7Nmo6gpM)

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

![](/files/-M9KsoGM-aEL1Pu_7vBk)

\
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.

![](/files/-M9Krj6giax3dxSjfkMb)

And that should be it. If the Rabbit sees the player (wolf), it will hide.
