Making AI run away from Player

So I have tried many different ways to get this to work.

What I am trying to do is simple.

Player walks into collision box attached to AI, which makes AI runs in the opposite direction away from player.

Red target is where AI stops at and the arrow is the direction he goes to get to it.

One way I attempted to do this was to use the get random point in radius node, I thought if its possible I could find a way to cut the radius in half and have it work as a half circle, and then the AI would run into a random spot in there. But I have not found a possible way to do this even through trying to get a circle diameter and such and calculating it with pi and other different combinations but no luck.

I also kept reading up about vectors and thought that they might be my answer but have not been able to put that to use either.

Any help would be much appreciated.


I am not sure right now how to make this work, but I would get look up rotation, reverse it, then pick a random location in X direction with a move to.

Might be a better way

Using Vector math is the best solution. You can get the direction of the by using the getlookAtRotation and rotating the AI’s forward vector (Not location or rotation) by that amount (essentially saying that this is the direction of the player relative to me.

From there you can use a cross product to find out what direction is 90 degrees from there. You could also use the dot product and some logic on the result. There are examples Herein cpp but it’s just math and results so it works in blueprints by plugging in values to nodes.

That will help give you a direction and then you can use the same types of checks (maybe with a random unit vector in cone of the inverse of the direction and a 40 degree cone to get the flee direction.

Hi, I’m actually using that for one of my games.

In the AI moveto node, I use the actor location instead of the actor pin. I break the pins and multiply the x axis(forward axis) to -1, giving you the inverse.
Pic added
P.S. I created a variable so I can set to 1 or -1. Why? So that I can make the AI run from and toward the player based on condition, i.e is being hurt, lost/gain advantage, etc.


Thanks for your help KinDaCreator. I will test your idea out, I in the meantime found a different solution to my problem.
What I did was in my actor blueprint in the view port I made a simple collision box and placed it far away behind the actor.
Then I edited it so that under certain conditions the AI will run to that box’s world location when it see’s the player.

I will try out your code for this though, my code is probably not the best way to do what I was trying to do. Vectors are hard.

Isn’t it possible to “Get look at rotation”, add 180 degrees around the z-axis, add a relative offset on x-axis, and set that as new location?

Simply set a Vector that pushes the AI 1000 in another direction away from the item you want to Avoid.

How You DO That?

Wouldn’t this potentially send the AI to an unreachable location though?

yep this

jep, that solution is flawed since it modifies the navigatable location without checking if the resulting location is navigatable aswell

I used a method where it would pick a location then work out the distance. if the distance is far enough they run, if not it gets another location until said result is obtained. Hope it helps!!

I wrote an AI Task that fires a line trace 8 times, in 45 degree rotations around the actor. Then it compares each collision (or end point) to find the furthest location from the player. This seems to work fairly well. It was difficult for me to keep up with the fleeing actor.

You could take this a step further and have it run another 8 line traces from each location it finds for a total of 64 possible locations. It would use the furthest navigable location from the player. I didn’t take it quite this far but it’s possible and there was no lag at all running 8 line traces.

Do you mind posting Screenshot?