我目前正在创建一个随着角色在世界范围内移动而更新的世界网格。世界始于生成一套3X3的预制件。当角色朝任意方向移动到远处时,它应沿相反方向摆脱行或列,在角色前面生成新的行或列,并将所有世界对象移回中心(以防止浮动)点错误)。这是我用来处理此问题的课程:
public class GridGenerator : MonoBehaviour {
//variables.
public int RowNumber;
public int ColumnNumber;
private Room[,] grid;
public LayerMask groundLayer;
//array of Gameobjects
public float roomWidth;
public float roomHeight;
public float unloadDistance = 200;
public Camera mainCam;
public List<GameObject> roomPrefabs;
Vector3 pos;
private void Awake()
{
GenerateGrid();
}
private void FixedUpdate()
{
int x, y;
if (CheckForShift(out x, out y))
{
Debug.Log(x + "," + y);
ShiftRooms(x, y);
}
}
private bool CheckForShift(out int xDirection, out int yDirection)
{
xDirection = 0;
yDirection = 0;
RaycastHit hit;
//pos = Physics.Raycast(mainCam.transform.position, mainCam.transform.forward, out hit, ~groundLayer) ? hit.point : mainCam.transform.position;
pos = mainCam.GetComponent<CameraController>().AveragePosition();
//float distance = Vector3.Distance(grid[x,y].transform.position, pos);
//if (distance > unloadDistance)
//{
if (pos.x < -20)
yDirection--;
else if (pos.x > 20)
yDirection++;
if (pos.z > 20)
xDirection++;
else if (pos.z < -20)
xDirection--;
//}
if (xDirection > 0)
xDirection = 1;
else if (xDirection < 0)
xDirection = -1;
if (yDirection > 0)
yDirection = 1;
else if (yDirection < 0)
yDirection = -1;
return (xDirection != 0 || yDirection != 0);
}
void ShiftRooms(int xDirection, int yDirection)
{
List<GameObject> toDestroy = new List<GameObject>();
Vector3 position;
Vector3 direction = new Vector3(roomWidth * yDirection, 0, roomHeight * xDirection);
if (xDirection > 0)
{
for (int i = 0; i < RowNumber; i++)
{
Destroy(grid[i,0].gameObject);
}
for (int x = 0; x < RowNumber - 1; x++)
{
for (int y = 0; y < ColumnNumber; y++)
{
grid[x,y] = grid[x + 1,y];
grid[x, y].transform.position += direction;
}
}
for (int i = 0; i < RowNumber; i++)
{
position = grid[i, ColumnNumber - 1].transform.position;
grid[i, ColumnNumber - 1] = AddRoom(i, ColumnNumber - 1);
grid[i, ColumnNumber - 1].transform.position = position;
}
}
else if (xDirection < 0)
{
for (int i = 0; i < RowNumber; i++)
{
Destroy(grid[i,ColumnNumber - 1].gameObject);
}
for (int x = 1; x < RowNumber; x++)
{
for (int y = 0; y < ColumnNumber; y++)
{
grid[x,y] = grid[x - 1,y];
grid[x, y].transform.position += direction;
}
}
for (int i = 0; i < ColumnNumber; i++)
{
position = grid[i, 0].transform.position;
grid[i, 0] = AddRoom(i, 0);
grid[i, 0].transform.position = position;
}
}
if (yDirection > 0)
{
for (int i = 0; i < ColumnNumber; i++)
{
Destroy(grid[RowNumber - 1,i].gameObject);
}
for (int x = 0; x < RowNumber; x++)
{
for (int y = 0; y < ColumnNumber - 1; y++)
{
grid[x,y] = grid[x,y + 1];
grid[x, y].transform.position += direction;
}
}
for (int i = 0; i < ColumnNumber; i++)
{
position = grid[RowNumber - 1, i].transform.position;
grid[RowNumber - 1, i] = AddRoom(RowNumber - 1, i);
grid[RowNumber - 1, i].transform.position = position;
}
}
else if (yDirection < 0)
{
for (int i = 0; i < ColumnNumber; i++)
{
Destroy(grid[0,i].gameObject);
}
for (int x = 0; x < RowNumber; x++)
{
for (int y = 1; y < ColumnNumber; y++)
{
grid[x,y] = grid[x,y - 1];
grid[x, y].transform.position += direction;
}
}
for (int i = 0; i < ColumnNumber; i++)
{
position = grid[0, i].transform.position;
grid[0, i] = AddRoom(0, i);
grid[0, i].transform.position = position;
}
}
Debug.Log(direction);
PlayerManager pm = FindObjectOfType<PlayerManager>();
for (int i = 0; i < pm.Players.Count; i++)
{
pm.Players[i].position += direction;
}
//for (int x = 0; x < RowNumber; x++)
//{
// for (int y = 0; y < ColumnNumber; y++)
// {
// if (grid[x, y] != null)
// grid[x, y].transform.position += direction;
// }
//}
}
void GenerateGrid()
{
grid = new Room[RowNumber, ColumnNumber];//new List<RoomList>(RowNumber);
//iterating over each row in the grid.
for (int i = 0; i < RowNumber; i++)
{
//grid.Add(new RoomList());
//iterating over each colulmn in the grid
for (int j = 0; j < ColumnNumber; j++)
{
grid[i,j] = AddRoom(i, j);
grid[i,j].transform.position += new Vector3(-20, 0, -20);
//grid[j, i] =
}
}
}
Room AddRoom(int x, int y)
{
GameObject roomToSpawn = roomPrefabs[Random.Range(0, roomPrefabs.Count)];
GameObject tempRoom = Instantiate(roomToSpawn, new Vector3(roomHeight * x - 20, 0, roomWidth * y - 20), Quaternion.identity);
tempRoom.name = "Room #" + x + "," + y;
tempRoom.transform.parent = transform.parent;
return tempRoom.GetComponent<Room>();
}
}
网格的当前行为很奇怪。似乎正确删除了正确的网格。然后,它会在不正确的位置生成新的网格碎片。当对象移动时,它们似乎在完全随机的方向上移动或根本不移动。此行为在同一时间在同一网格上不一致。