Ai running into a wall when fleeing from player

I was able to program an AI, using sight perception, to run away from the player by using the vector direction between both locations of the ai and the player character. The issue I’m running into is that when the ai runs into a wall, it has no other place to go to, because its next location would be beyond that wall.

(The pic below shows the overall running away mechanic, it turns the vector between the player and the ai into a direction vector, which then is applied to the ai’s world location by a magnitude of 200 units. This makes it so that the ai always runs away from the player depending on where the ai is in respect to the player)

I tried to implement a TraceSphereByChannel to check if there is an obstructing object (a wall) in front of the ai. Then I was able to tell it to take a sharp 90 degrees turn whenever it detects a wall in front of it.

(here I created a vector generator that finds a random vector that is 90 degrees from the original direction of where the ai is running. I had that vector be the new vector the ai would run towards)

After all of that, the problem ended up being that the ai ends up zigzagging along the wall as its destination shifts consecutively from running into the wall to running at 90 degrees from its original direction.

So I am kinda lost at this point, because with the system that I have implemented, I dont know how to tell the ai to keep running away from the player but to also avoid running into walls. Maybe there is a better ‘run away’ blueprint that would make it easier to fix this issue? Otherwise, if anyone can help me find a solution using what I’ve made so far, I would be very grateful !

The zigzagging probably happens because it’s calculating the location too often. You might be able to solve this particular issue with a cooldown decorator, so once you decide it should run 90° from it’s original direction, the cooldown has to pass before it changes back to original again. If it works how I think, it would of course still only reduce the zigzagging frequency, but might provide better results.

Making some more immersive running away / fleeing behavior is somewhat more complex I think.

I guess you should always try and work with navigable or even reachable points. It also highly depends on the number of obstructive objects and their density. I wouldn’t go with detecting the wall anyways, as your target location would potentially be way to close to the player. Maybe calculate a location 10m in the fleeing direction, then try and get a random reachable point in a close radius there. You could do this in a loop and increase the radius if you don’t find any points (the boolean output of the GetRandomReachablePointInRadius). If your radius reaches an upper limit, maybe because the AI reached the map border, it should choose some other way of finding a location, maybe try the same thing to forward-left / forward-right.

I am no specialist on this, that’s just how I’d try and make something like this.