Exemplo n.º 1
0
 Vector2 CorrectStart(Vector2 start)
 {
     Vector2[] pointsToCheck = new Vector2[4] {
         new Vector2(GameManager.coordinateSize.x, 0), new Vector2(-GameManager.coordinateSize.x, 0), new Vector2(0, GameManager.coordinateSize.y), new Vector2(0, -GameManager.coordinateSize.y)
     };
     foreach (Vector2 pointToCheck in pointsToCheck)
     {
         Vector2 point = UF.FromCoordinatesToWorld(UF.CoordinatePosition(start)) + pointToCheck;
         if (IsInsideMap(point) && !UF.CheckForMapCollider(start, point))
         {
             return(point);
         }
     }
     return(start);
 }
Exemplo n.º 2
0
    public List <Vector2> FindPath(Vector2 start, Vector2 goal)
    {
        if (!IsInsideMap(goal))
        {
            return(new List <Vector2>()
            {
            });
        }
        GameObject mapColliderUnderGoal = UF.FetchGameObject(goal, LayerMask.GetMask("MapColliders"));

        if (mapColliderUnderGoal != null)
        {
            Vector2 closestPointFromCollider = mapColliderUnderGoal.GetComponent <Collider2D>().bounds.ClosestPoint(start);
            goal = closestPointFromCollider + (start - closestPointFromCollider).normalized * 0.01f;
        }
        if (!IsInsideMap(UF.FromCoordinatesToWorld(UF.CoordinatePosition(start))))
        {
            start = CorrectStart(start);
        }
        List <Vector2> path = AStarPathFinding(UF.CoordinatePosition(start), UF.CoordinatePosition(goal), goal);

        return(path);
    }