How would I set AI speed based on distance from player

Hi,

Just wondering if anyone has any examples of how I can slow down an AI based on how close he is to my character?

Scenario, I have an AI with BT currently following my player, The AI Max Speed is 400 and the Player Max Speed is 300. I am basically on pressing a key (F in this instance) setting the AI to follow my player, then when it is within 100 units the AI stops and waits for a couple of seconds then repeats the process.

What I want to do is get the AI to run to the player but slow down when he is within 100 units to match player speed (I.E. change the AI speed from 400 to 300) but I want to do this gradually so it is smooth and not a jerky change. then the AI should just continue to follow the player until the Player stops. At that point the AI waits in place. If the Player moves off again, the AI randomly picks a time between 0.5 and 3 seconds, then re loops his follow.

Has anyone done anything like this and could share some screen shots?

You can run a Service in the behavior tree that checks the distance to the player, and adjusts the character’s walking speed…

HI, @eblade thanks for the reply.

I have an odd issue with this, for some reason (running a print) the distance value is getting greater when my Ai is closer to the player, should it not become shorter?

Start distance was 351.79 as the AI got closer to the player you can see the value is getting bigger, not smaller.
image

My guess would be that “Owner Actor” isn’t what you’re expecting it to be (or you’re doing multiplayer, and it’s a different character that you should be getting distance to?) …

If you use Receive Tick AI, you’ll have a connection for the Controller and the Character, so you’ll know for sure what actor you’re using… that would be the first thing I’d try, Receive Tick AI, and connector Character input to the Get Distance To instead of using Owner

Tick AI did the trick… silly mistake from me. Thanks for the assist.

1 Like