Right now I have my AI set up where when they enter aggro mode based
on pawn sensing or player attacking from behind. Once in aggro mode
they follow the player and then attack until either the AI or player dies.
I attached screenshots of my controller and behaviour tree.
I want to have the AI stop chasing the player and return to
his patrol if the distance between the AI and the player is
greater than 1000cm. But I want the AI to still be able to
go back into Aggro mode if the player comes within sight
or attacks again.
I was thinking maybe a timer to be running while the
AI is in pursuit of the player to keep checking the distance.
And then another decorator, isPlayerStillInSight to determine
what to do.
But I would love to hear suggestions.
Does anyone know, What is the best way to set this up?
Here’s one way to approach it, psuedo-BehaviorTree of the patrol state, then goes into a get closer state (if within FOV but not engagement distance), and then into the attack state if within FOV + engagement distance.
There is some green text on the selector “Within FOV + DistCheck”. Is that a decorator, a function call, something else, or just a comment/tag?
You have decorators on each sequence “state ==” for various states. Are these decorators based on the values of Bools being set as keys or are they variables of a different type?
In your example I can’t see how the AI will get out of the state “detected”. How will the decorator variables be reset if player moves out of range?
It’s a service, it would just constantly get forward angles on the AI and seeing if it **Spotted **the Player within FOV with a clear line of sight via a trace, store that origin in the location vector BlackBoard key and tell the AI to go there. If the Player is within engagement distance then Player is **Detected **so run closer to him and with a delay for aiming attack him. Otherwise go back to Patrol if none of the above.
You’ll have to mix in the distance to stop chasing (or even spot the Player at all) how you see fit within those conditions for updating which state to change to.
“state” would ideally just be an enum BlackBoard key with Patrol, Spotted, *Detected *entries. The service above would be constantly updating the “state” variable, and the decorators on each sequence checks it.
If the Player runs around a corner the AI will go to the last Spotted origin (of the Player), then look around for a few seconds and determine whether to go back to **Patrol **or continue chasing in **Spotted **or attack if Detected.
I have been working on this all day. I am attaching what I have at the bottom
of the post.
It works but I don’t really like it. And it doesn’t really do exactly what I want.
He chases the player on site, and then gets close to him and attacks
after his attack he pauses to give the player a little time to run away
if the player gets far enough he stops attacking.
I like your solution much better because with my solution, he needs to
reach the player at least once and attack and the player needs to
run away exactly after the attack. It is not as good as your solution.
Your solution achieves what I want, if you run far enough he stops
chasing. That is what I want. I tried to do it with Function calls
after wasting about 10 hours I see why I need that Service.
I never set up a service on the behavior tree
and I don’t know how to set up an enum variable
Can you post the code for the service and setting the enum?
or point me to somewhere I can learn how to do that?
On your AI character add an enum variable BotState and point the Variable Type to **EBotState **you made earlier.
Then switch over to your BlackBoard create a new enum variable, I called it BotState too. Point the Enum Type to **EBotState **you made earlier
The top left is how to set the BotState on the AI character, and below that is how to use a switch if you want to do some script stuff based on what the current value of the **BotState **variable is.
To the right is how to set the BlackBoard **BotState **variable, which is what you’ll use on the Decorators to check which sequence to go down. Basically the *Key Name *should equal the name of the Blackboard key we want to change, which is BotState
And this is how you check the BlackBoard **BotState **on the Decorator
After spending my entire weekend on something I thought would take an hour.
I got depressed about this and avoided working on it for a day or two.
Anyway, I rolled up my sleeves and tried again today and was finally able to get it to work exactly like I wanted.
thanks mostly to your help.
Now the enemy will start to go after the player based on PawnSensing
he will continue to purse the player as long as the player has not moved out of range
if he moves out of range he goes back to patrol.
I followed your guidance. The service and the Enum were great suggestions.
The only thing I deviated on was sending the blackboard a ‘Player spotted location’ and having AI
chase the location you were at. I had done a BT that did that in a different project and I didn’t like
it at all. They were chasing shadows and easy to avoid. My goal was to have him go right after the
player unless the player really runs away very far. (and to do this without giving every bot a leash or
a predefined zone)
The service calls the function that checks the distance, sets isInRange, and then calls a function setBotState
(which I posted a screenshot of)
setBotState will do nothing if he is Patrolling, but if he is not patrolling, then it checks if he is still in range.