A* Pathfinding Project

RichAi - How to check when target is unreachable and then reachable?


#1

Hi,

The player can get on top of rocks or other things in my game and I want to detect when that happens. Note that this code is only called when the enemy couldnt reach the player (Im using behavior trees and this is a separate task from the Move).

// Update loop

        _astarAi.destination = _player.position;
        _astarAi.SearchPath();

        if (_richAi.reachedEndOfPath)
        {
            Debug.Log("Cant reach player"); 
            return TaskStatus.Running;
        }
        return TaskStatus.Success;

This works and if I jump from the rock and start moving the enemy will start following me again, the problem is when I land very close to the enemy, and _richAi.reachEndOfPath still returns true even though Im in front of the enemy.

I have tried doing this, but still it cant detect when the player jumps from the rock and lands right in front of the enemy, do you have any ideas what else I can try? Also the Y position of the enemy and the player have a difference, maybe there is another way to better solve this problem?

        _astarAi.destination = _player.position;
        _astarAi.SearchPath();

        if (_astarAi.reachedDestination)
        {
             Debug.Log("Reached player");
             return TaskStatus.Success;
        }

        if (_astarAi.reachedEndOfPath)
        {
            Debug.Log("Cant reach");
            return TaskStatus.Running;
        }
        return TaskStatus.Success;

#2

Wouldn’t it be easier to add a custom loop to check the distance to the player?

if (Vector3.Distance(transform.position, _player.position < distanceTreshold) {
                //Reached Player
}

#3

I found this to check if the player is standing on an unreachable node

    private bool IsTargetReachable(Vector3 targetPos)
    {
        var graph = AstarPath.active.data.recastGraph;
        var targetNode = graph.PointOnNavmesh(targetPos, null);

        return targetNode != null;

    }