# Make Animal flee when see the animal Player

## Overview

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.

{% 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)]

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KQjW_mmH91Vg9B-5u%2Fimage.png?alt=media\&token=23d23130-6d2f-472b-8f01-e60252988804)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kd-AHGlrzDvcHdBl5%2F-M9KfETU_DCQC0JXgJl8%2Fimage.png?alt=media\&token=fe9732b0-376f-4ca6-a18d-009d4559aa82)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kd-AHGlrzDvcHdBl5%2F-M9KfWsATjpKh9zEjm52%2Fimage.png?alt=media\&token=d8767344-fdb9-43d0-a159-f7d5e08213a9)

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 %}

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KSEBFIZr8tJ1KNWZI%2Fimage.png?alt=media\&token=60f181d8-5c94-4d45-a721-4b76f4391c55)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KSLupp-EsrrCE13ld%2Fimage.png?alt=media\&token=2acd58a2-9d45-49d8-9b91-bfb3ffae2e79)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KSStHuJEzXJyDV16X%2Fimage.png?alt=media\&token=11c82d9d-526e-41c7-9fc4-9251938735f0)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KSwaeiuhv179T7RKp%2Fimage.png?alt=media\&token=a151d76c-664f-4c87-afbf-cf47b47efc64)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KUFbgTKjPb7fRH2Gf%2Frr8ISKorsE.gif?alt=media\&token=0097eed9-c8c0-4ce9-8648-a580d69c9fad)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KUnLl5WKVpXqf3B4L%2Fimage.png?alt=media\&token=89eff183-1099-4e1e-a070-c80c6c5f81a0)

3- Connect the waypoints together to make a route

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KV7v8WLGqVYS6-9Ej%2Fs0GC2kvQbb.gif?alt=media\&token=c8e369b7-ad95-4cec-a92c-448f8893a106)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KVFRY9OkXdvkBnLFB%2Fimage.png?alt=media\&token=cff586eb-8397-4d76-bc48-0af0bc9a8271)

## Hide Spot Zone

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9GIWcEFl_yy9AYCxLp%2F-M9KW7P365_I9pTEHem7%2Fimage.png?alt=media\&token=2c46480a-a4c7-4fe2-9ba5-258cc10fc7ed)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kg6LiTAoFXTcRdfRO%2F-M9Kgf4w_nGh2L4nzyVc%2Fimage.png?alt=media\&token=f52acc2c-c487-425c-ac75-21bc66def877)

![Rename the New create Tag to "HideSpot"](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9KXlk73MbdyUapWzNQ%2F-M9KYhC2UbS9afhcQS_q%2Fimage.png?alt=media\&token=0d52e159-1495-4436-a582-1918975157e1)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9KXlk73MbdyUapWzNQ%2F-M9KYxwTTH-NlADOZ1r0%2Fimage.png?alt=media\&token=be53593d-2b33-4cd0-a28c-6d7748ce88d0)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9KXlk73MbdyUapWzNQ%2F-M9KZycJwF7qpUwo5Mju%2Fimage.png?alt=media\&token=29685031-4c2a-441a-8c83-09a89dc73b89)

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.&#x20;
* 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.&#x20;
* When it finish waiting it will return to the First state.

1- Create the States and give them proper names:

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9K_B20ok3k18vhxeTf%2F-M9KbtCgJtW7ZmzBOEDU%2Fimage.png?alt=media\&token=94e4723d-200a-49af-8dcb-2773538a2e4d)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9K_B20ok3k18vhxeTf%2F-M9KbbLi0ELeUUo7ApmX%2Fimage.png?alt=media\&token=35a30dc6-225c-461a-ba0a-4425d1ec72c9)

## Creating the AI Decisions

We need 3 Decisions:&#x20;

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kd-AHGlrzDvcHdBl5%2F-M9KfzqaRz7qp_CqAPMD%2Fimage.png?alt=media\&token=67bcade0-3e4b-4dac-8058-9f88d02bad11)

1- Create a new **Look Decision** "Look for the Player".&#x20;

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kihv8nqbbNvzDUjgE%2F-M9KrB2QnMYjQjFp-NsB%2Fimage.png?alt=media\&token=4f53424d-a016-4b75-ab2d-5ab9169d9023)

{% 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 %}

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kd-AHGlrzDvcHdBl5%2F-M9Ken1YEJC2KIINTBSO%2Fimage.png?alt=media\&token=ad29c25a-9c42-49ff-b3c3-91e52352c2f4)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kg6LiTAoFXTcRdfRO%2F-M9Khl8y44nKvUW2sCLi%2Fimage.png?alt=media\&token=fe4d0ba1-a527-4554-b13a-23b31312b0d8)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9KruRjoYBrM29uRyTu%2F-M9KsjXvqSXxnlk_jt1i%2Fimage.png?alt=media\&token=26063639-eba3-4ac8-95e0-865b3a063f01)

## Setting up the AI States with Decisions and Tasks

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9KruRjoYBrM29uRyTu%2F-M9KsBgxn6bVKk_E1JT2%2Fimage.png?alt=media\&token=1c847578-988a-43d1-b451-1014d9ce8e6a)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kihv8nqbbNvzDUjgE%2F-M9KqIK2r11r7Nmo6gpM%2Fimage.png?alt=media\&token=18ee3c6d-11d6-46f7-a837-1e05b253f689)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9KruRjoYBrM29uRyTu%2F-M9KsoGM-aEL1Pu_7vBk%2Fimage.png?alt=media\&token=bffc4d89-b230-4b8a-a58b-bfb1fb857bfa)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M9Kihv8nqbbNvzDUjgE%2F-M9Krj6giax3dxSjfkMb%2Fimage.png?alt=media\&token=f318da08-f5f7-469f-ba5a-68fb9ea7e0ca)

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