Esempio n. 1
0
    float radius = 2.0f; //radius of capsulecast

    //This function will generate a 2D array of either NULL or MAXINT depending on whether there are
    //obstacles in the way or not.
    public int[,] Generate_grid(float cellSize, int row_size, int col_size, int xOff, int zOff)
    {
        xOffset       = xOff;
        zOffset       = zOff;
        grid          = new custom_grid();
        grid.cellSize = cellSize;

        int[,] obstacle_grid = new int[row_size, col_size]; //create an empty grid to load obstacle values into


        //col is col index, row is row index
        for (int col = 0; col < col_size; col++)
        {
            for (int row = 0; row < row_size; row++)
            {
                Vector3 half_offset = (Vector3.right * 0.5f * cellSize) + (Vector3.forward * 0.5f * cellSize);
                //create a new ray from 100 units up pointing down towards the map
                Ray ray = new Ray(grid.cellToWorld(row + xOffset, col + zOffset) + half_offset + (1000.0f * Vector3.up), Vector3.down);

                Vector3 origin = grid.cellToWorld(row + xOffset, col + zOffset) + half_offset + (1000.0f * Vector3.up);

                //if (Physics.Raycast(ray, out hit, 50000.0f,1<<10)) //check a raycast out to infinity
                if (Physics.SphereCast(origin, radius, Vector3.down, out hit, 50000.0f, (1 << 10) | (1 << 14)))
                {
                    //return the GameObject we hit
                    go = hit.transform.gameObject;
                    obstacle_grid[row, col] = Int32.MaxValue;                               //Max value represents impassable object
                }
                else if (Physics.Raycast(origin, -Vector3.up, out hit, 50000.0f, (1 << 8))) //check if it is not an impassable slope
                {
                    if (Vector3.Angle(Vector3.up, hit.normal) > 5.0f)
                    {
                        obstacle_grid[row, col] = Int32.MaxValue; //Max value represents impassable object
                    }
                    else if (hit.point.y < 3.8f)
                    {
                        obstacle_grid[row, col] = Int32.MaxValue; //Max value represents impassable object
                    }
                }
            }
        }

        return(obstacle_grid);
    }