Example #1
0
 public void AddHeight(int x, int y, float height)
 {
     if (grid.InBounds(x, y))
     {
         heightObstacleMap[x, y] = heightObstacleMap[x, y] + height < 0 ?
                                   heightObstacleMap[x, y] = 0 :
                                                             heightObstacleMap[x, y] + height;
     }
 }
Example #2
0
    protected Vector2Int RandomWalk()
    {
        Vector2Int randomPos = new Vector2Int(0, 0);

        // Loop until a traversable straight line is found
        // It must not pass an obstacle and it must not be out of bounds.
        // This might cause problems if an agent is surrounded by obstacles, for instance
        // In that case: do something more intelligent.

        while (true)
        {
            Vector2 randomWalk = Random.insideUnitCircle * Random.Range(0, maxRandomWalkDistance);
            randomPos = grid.WorldToGrid(transform.position + new Vector3(randomWalk.x, 0, randomWalk.y));
            // Ignore out of bound positions
            if (!grid.InBounds(randomPos))
            {
                continue;
            }

            // Ignore obstacles
            RaycastHit hit;
            if (Physics.Raycast(transform.position, new Vector3(randomWalk.x, 0, randomWalk.y), out hit, randomWalk.magnitude, LayerMask.GetMask("Obstacles")))
            {
                continue;
            }
            else if (obstacleHeightMap.GetHeight(randomPos.x, randomPos.y) > 0)
            {
                continue;
            }
            break;
        }
        return(randomPos);
    }
Example #3
0
 public bool InBounds(Vector2Int pos)
 {
     return(grid.InBounds(pos));
 }