make ai run away

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zen3001

I know how to find paths and make ai chase a target (or the player) with either astar or the standard navigation but how do you find a path to run away from a target?

:bust_in_silhouette: Reply From: Lopy

To run away from something, you can run towards a target that is far away, like a corner of your level.
Alternatively, if the AI is scarred, you could just have it go in the opposite direction of what it is scarred of, until it hits a wall head first.

thought about this before but how would I find out these cornor positions?

zen3001 | 2021-02-03 18:14

Hi, I have somthing like this:

public void TouchByLight(Light3D light)
    if (State != EnemyState.ESCAPE)
        State = EnemyState.ESCAPE;
        Vector3 direction = new Vector3(
            light.GlobalTransform.origin.x - GlobalTransform.origin.x,
            light.GlobalTransform.origin.z - GlobalTransform.origin.z
        Target = GlobalTransform.origin + direction * ESCAPE_DISTANCE;

This should work as you say, but the AI run to a random direction in this case, have you any Idea of what I could doing wrong here?

PowerPotato | 2023-01-14 10:54

:bust_in_silhouette: Reply From: PowerPotato

Hi, after I struggle by myself with this problem, I finally end with this solution:

 public void TouchByLight(Node3D escapeFrom)
        Vector3 direction = GlobalTransform.origin.DirectionTo(escapeFrom.GlobalTransform.origin);
        Vector3 target = GlobalTransform.origin - direction * ESCAPE_DISTANCE;
        target.y = GlobalTransform.origin.y;
        Target = target;


Hope this help