AI & Player Distance Detection Problem

Hello. I’m trying to figure out how to detect the distance between my AI and player in the blackboard tree. I want to put a “Is Far Enough?” node down to detect if the AI is too far from the player and choose an alternative path near the player. I’ve been stuck on this for almost a week. Some help would be greatly appreciated.

Hi, you could make a custom decorator [HR][/HR]
I don’t know how much understanding you have about behavior trees and AI, therefore two ways:

(1) fast and easy to setup, but I would not use it in the long run

(2) will take a bit longer and would need a bit more understanding to setup, but you can set the target actor as variable, so that’s how I would do it

Let me know if you need more help :slight_smile:

1 Like

Hello.
Just for extra context, I’m not a master at behavior trees and AIs. I’m still learning but I know the general basics and somewhat difficult things in behavior trees. I already have my AI chasing the player, roaming, looking for the player, and other random stuff that’s needed. It’s just the distance checking that’s bugging me.

I tried to follow what you suggested but I kind of got lost. Either that or I did something wrong. I probably need more explanation and/or context.

The code from the first image works as is, you can just copy it and it should work. It returns true, if the distance between the AI and the player is greater than the input distance and false otherwise. Therefore the code you execute in the Behavior Tree (BT) after this decorator node will only execute if the distance between the AI and the player is greater than the distance input to this decorator.

To create a new decorator you would first click “New Decorator” inside the BT and then inside this newly created decorator you would either override the “PerformConditionCheck” or like in the two images above the “PerformConditionCheckAI” function.
[HR][/HR]
The code from the second image uses a blackboard key instead of getting the player. Therefore it will work with any actor and is not limited to work only with the player. But you would need to set this blackboard key somewhere.

It took some time but I eventually got it; It works exactly how I want it to.
But there’s one more problem I have…

When it finds an alternative route, I need it to chase the player again after a few seconds of activating the “find alternative route” tree. If it doesn’t abort itself, it will find alternative routes eternally.

What do I use to cancel its “find alternative route” tree so it can go back to chasing the player? I tried using a “TimeLimit” decorator to abort itself and a “Force Success” decorator later to see if that would work but those didn’t seem to work. Either that or I’m using them wrong.

For extra information, I added a cooldown so it doesn’t constantly find alternative routes. I don’t know if it’s related but I thought I’d add that detail just in case.

Can you show an image of your behavior tree?

And what does “find alternative route” do?
[HR][/HR]
But generally speaking use a selector. A selector will execute its children from left to right until one succeeds. And at the end of your “find alternative route” tree you build in a task that fails. Therefore the whole “find alternative route” will fail after finishing execution and therefore the selector will execute the next branch.

I’m extremely sorry that I did not get back to you in so long. I’ve been doing a lot and couldn’t get back to you sooner. I hope you can accept this apology.
Though I wanted to thank you for helping me because after a few breaks and trial and error, I managed to get the AI working! It now tries to cut the player’s path when the player goes too far from the AI.

Again, thank you for helping me. I’m not sure if I could continue working without your knowledge.

Sure, absolutely no problem =)