# Simple AI with Behavior Trees - Agro?

**URL:** <https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678>\
**Category:** AI\
**Tags:** Behavior-Tree, unreal-engine, question\
**Created:** [January 11, 2015, 11:11pm UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678 "2015-01-11T23:11:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_e50a434a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e50a434a](https://forums.unrealengine.com/u/anonymous_user_e50a434a)\
**Post date:** [January 11, 2015, 11:11pm UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/1 "2015-01-11T23:11:59Z")

</div>

Hi everyone!

So im struggling a bit to make AI, and my struggle is within the Behavior Tree. Im managing to get the AI to follow the player or patrol around the navmesh - basically i’m able to get the AI to execute simple tasks. However, what I want to do is to make the enemies patrol and when the player is within a certain agro range, the AI should follow the player.

How would one manage the behavior tree in this cituation? I’ve tried all the tutorials I could find but I can’t get it to work. I should mention that i’m doing it in the third person example project.

TL;DR - How would I structure the behavior tree when I want AI to patrol around and then execute another task when the player is within a certain range.

Thanks for reading!  
Best regards,

---

<div class="post-metadata">

**Author:** ![PeterLNewton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PeterLNewton](https://forums.unrealengine.com/u/PeterLNewton)\
**Post date:** [January 12, 2015, 2:02am UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/2 "2015-01-12T02:02:28Z")

</div>

Hi \*\*\*\*,

To make this as simple as possible! What I suggest is a sequence node immediately after your Root node. This will allow you to control you execution, and prevent another node from being executed before the previous one finishes.

So in our case we create the first node **Attack!** which has a Decorator **Found Enemy?**. This node only executes if an enemy is found. Once an enemy is found, the node shouldn’t finish execution until the enemy is lost. I suggest putting this logic on the Pawn itself. So the enemy check is done by the _Pawn_, so the _Decorator_ gets that information from the _Pawn_. Once the enemy is found, the **Attack!** node assigns a _Event Dispatcher_ which is for your **Enemy Lost** Event on the _Pawn_ itself. Once the enemy is lost, _Pawn_ calls **Enemy Lost**. The node **Attack!** with the _Enemy Lost_ Event Dispatcher Assigned is called finishing execution.

The next node is straight forward, this state gets the next route and moves towards it. Main node should be sequence to allow proper flow execution. Current route information should be stored in the Blackboard Asset.

So the idea is, should I be attacking now? No, keep patrolling. Usually this is the approach for FPS. _Shoot or Don’t Shoot is the question._

 ![26368-simpleattackpatrol.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/9/0a9e20609f46127487c24d690ddd709b068a8297.jpeg)

Thanks,

Peter L. Newton

---

<div class="post-metadata">

**Author:** ![MieszkoZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MieszkoZ](https://forums.unrealengine.com/u/MieszkoZ)\
**Post date:** [January 12, 2015, 10:00am UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/3 "2015-01-12T10:00:51Z")

</div>

I agree, I’d just use a _Selector_ where Peter used _Sequence_, and make the Patrol a _Sequence_. The main reason for it being that with the original setup you’d end up with the whole sequence failing if “Found Enemy?” failed, unless you added a kind of “always succeed” decorator on top of it. Needless complication. Use this:

 ![26376-selector.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/1/9/d1914c7e38bf4fb2f1ccae74ea3ade90713a4a74.png)

I’d also suggest making the “Found Enemy?” condition simple Blackboard-based check if your `Enemy` key is set or not (and you course you’ll need to be writing your AI’s enemy information into blackboard).

Cheers,

–mieszko

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_e50a434a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e50a434a](https://forums.unrealengine.com/u/anonymous_user_e50a434a)\
**Post date:** [January 12, 2015, 12:19pm UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/4 "2015-01-12T12:19:58Z")

</div>

Thank you both very much!

Just a quick question regarding the chart, what node should the found enemy/attack be? Same goes for patrol, is it a selector with a decorator or?

---

<div class="post-metadata">

**Author:** ![PeterLNewton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PeterLNewton](https://forums.unrealengine.com/u/PeterLNewton)\
**Post date:** [May 12, 2015, 5:17pm UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/5 "2015-05-12T17:17:52Z")

</div>

Glackie – were you able to figure everything out?

---

<div class="post-metadata">

**Author:** ![terryjoel1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@terryjoel1](https://forums.unrealengine.com/u/terryjoel1)\
**Post date:** [June 12, 2021, 10:26pm UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/6 "2021-06-12T22:26:20Z")

</div>

from my many months of searching no one can make a good aggro system any were its just not possible would love for someone to proof me wrong

---

<div class="post-metadata">

**Author:** ![notlayenem](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@notlayenem](https://forums.unrealengine.com/u/notlayenem)\
**Post date:** [May 1, 2023, 7:14am UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/7 "2023-05-01T07:14:10Z")

</div>

Now I’m over here searching for ANY reference to building any agro system hahaha the years are not kind!

---

<div class="post-metadata">

**Author:** ![BIGTIMEMASTER](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bigtimemaster/32/710746_2.png) [@BIGTIMEMASTER](https://forums.unrealengine.com/u/BIGTIMEMASTER)\
**Post date:** [May 1, 2023, 5:11pm UTC](https://forums.unrealengine.com/t/simple-ai-with-behavior-trees-agro/303678/8 "2023-05-01T17:11:34Z")

</div>

Here’s how I am doing enemy behavior. I don’t use behavior trees, just blueprint:

A timer drives updates for enemy behavior:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/3/f/43fac87b66ed365f777cd75f3d601d694b2e0303.png)

Enemies are held in an array.

First we gather all data necessary to make decisions. This is typically a bool. Can See Player? Close Enough to Attack?

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/8/2/78242995758f253c0665333d39d2a495622af98d.png)

If there is line of sight and we are within the enemies field of view, then enemy knows player location:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/f/5/1f522dda8656b03205dead9f6bb047604c54c3c5.png)

In field of view?:  
Convert dot product of player location to enemies forward vector to degrees, and make sure it is within a certain threshold. (e.g. ~90-140)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/9/0/f9048c504dce4d5469bff612f6b45c10daba04dd.png)  
Note the Select on Combat Stance. If my player is in prone position, then I just make the enemies FOV smaller, thus making it less likely to detect the player. I prefer simple logic like this instead of complex simulations, because the only thing the player knows is how likely they will be seen. No reason to over-complicate!

Line of sight?":  
A line trace from enemies head bone to the player. I use a max distance so that enemies wont spot you from across the level.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/8/b/e8bd869ee847a787f35077ca28db835ed4951d82.png)

In that case, Is Alerted (e.g. aggro’d) is also true. In my game, I only set this once. Once aggro’d the enemy stays aggro’d.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/a/e/4ae4de0058632e3c908ed0b5d6d7239fabd1cddc.png)

After data is gathered with which to make decisions with, then we can make those decisions for each enemy:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/3/1/3312de34dbad670992287211dacb9b8b9fde1b7b.png)

This is just handled by priority:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/0/6/b06d5c825e8a03ab7d5d853d20935abd1d522941.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/3/8/b38c2fce9bab09bae2cba3617cea2b09d2a002bd.png)

Depending on the type of enemy, I branch behavior into a few varieties:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/8/e/f8ecbd033c000debf6b65ae3f25fb7a81f3cb105.png)

Continuous means that the timer update will keep updating the state. For instance, if we are charging the player, we want to keep updating that so that the enemy tracks players position fast enough.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/5/d/75df287316041aa3d69942d1fee5d2ba04547e75.png)

But if the enemy is currently attacking (playing an attack montage), then we want to gate that off so that we don’t keep calling the montage too frequently. So when the attack montage starts we set a bool to close the gate, and when the montage finish the bool is flipped. Then another attack can happen if we are still going down that branch.

I recommend trying to build a simple system like this first before trying to work with an extremely robust system like behavior tree. For a beginner, BT would be like trying to learn how to drive by piloting an F-22. With a simple blueprint only setup like this, you can avoid the complexities of passing data between multiple classes, and also you can most readily see and understand the execution flow. Whereas if you work with a system built by somebody else, there is a lot of stuff happening that is hidden and you won’t understand.

I find it helpful to verify my logic carefully before diving into code:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/9/a/99a434a31f3ae3f56d33e2e5259249c8dd3cb449.png)

If you sketch it out like this and then imagine the execution flow going down each branch, you can catch your logic errors before getting into the weeds with code.
