AI Behavior Tree

I have created an enemy Ai controlled by a behavior tree.

Basically it patrol randomly but when it see the player run towards and attack it, it works fine when I have one enemy on the level and sometimes with multiple enemies, but most of the time I got an error message with multiple enemies.

The enemy has a sphere collision which if the player walks in it can see the player.

I think the problem is with the SeePlayer boolean because it tries to set it multiple times but I can’t fix it.
I tried to add a Branch / Is Valid nodes to first check if the SeePlayer already set or not before it set it, but still got the message.

Hey there!

Here is what I’d do to receive the results that you want:

First of all, I like to use multiple Behavior Trees for differing AI States (for e.g - Roaming/Attacking/etc.). So, I’d go ahead and create an Enumerator containing these AI States.

Then create a Blueprint Interface called BPI_ChangeAIState, which will contain just one function. Add the enumerator we’ve created to the INPUT of the function:
image

To continue, create an AI Controller and assign it to the AI character:
image

Inside the AI Controller, go to Class Settings and assign the Blueprint Interface:
image

Then go ahead and create your Behavior Trees & Blackboard. Be sure that the Blackboard is selected correctly in the Behavior Trees. In this case, I’ve created 2 behavior trees - one for Roaming and one for Attacking:
image

In the AI Controller, create a variable - “CurrentAIState” and set its variable type to be the enumerator we’ve created:
image

Under the functions tab, locate Interfaces and implement the “Change AI State” function by right clicking. Then, you can do something like this to run multiple trees according to the AI’s state:

Next, hop into the Blackboard and create 2 variables - “PlayerTarget” Object & “RandomLocation” Vector:
image

Set the “PlayerTarget” object variable base class to “Actor”:
image

Now that we’ve got that part done, we can now add an AIPerception Component to the character itself:
image

Next, setup the component like this:

And then right-click the AIPerception component and override “On Perception Updated”. Here, you can check what targets the AI sees, and accordingly, set blackboard values (in this case, we set “PlayerTarget”):

Quickly whip out some basic logic in the Roaming behavior tree:

The BTT_ChangeAIState task (create a variable “NewAIState” which is instance editable with the type of the enumerator we made at the first step):

The BTT_GetRandomPoint task (promote the radius to a variable & also create a blackboard key selector variable which is instance editable):

Next up, the Attacking behavior tree:

This should work properly! You can tweak out the AIPerception settings to your liking. Also in the “OnPerceptionUpdated” event, you can check if the perceived actor has a specific tag (such as “Player”) before setting the blackboard value for more accurate results.

Also, you can use a timed event to clear the “PlayerTarget” blackboard value when the AI loses sight, so it feels a bit more smarter :slight_smile:

Hope this helps!

Sorry for the late answer. It works as I wanted. Thank you for the help.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.