Hello, so I have a procedurally generated terrain and I load it and unload it as the player moves.
The thing is that I have to know in which direction my player is moving in order to load terrain and unload terrain efficiently.
I actually know how to get the direction I am moving, it’s just that it doesn’t seem to work… I multiply the functions CalculateMovementX() and CalculateMovementZ() with some coordinates in order to get the position desired based on the direction I am moving. I think this is not accurate enough because sometimes the tiles that need to be deleted are not deleted (You will see in a pic down below)…
I will be leaving all the important code for you to understand what I am trying to say.
private void Update()
{
if (Mathf.Abs(CalculateMovementX()) < tileSize && Mathf.Abs(CalculateMovementZ()) < tileSize)
return; //IF THE PLAYER HASN’T MOVED 1 FULL TILE YET, RETURN
GenerateTerrain();
}
private int CalculateMovementX()
{
return (int)(Mathf.Floor(character.transform.position.x) - previousPosition.x);
}
private int CalculateMovementZ()
{
return (int)(Mathf.Floor(character.transform.position.z) - previousPosition.z);
}
private void GenerateTerrain()
{
if (Mathf.Abs(CalculateMovementX()) > 0) //IF PLAYER MOVES HORIZONTALLY
{
placementCoord.x = Mathf.Floor(character.transform.position.x) * tileSize - ((gridWidth / 2) + 1) * CalculateMovementX();
selectedTile.x = (int)placementCoord.x;
//LOOP TO DELETE OLD TILES
for (j = 0; j < gridHeight; j++)
{
placementCoord.z = Mathf.Floor(character.transform.position.z) - gridHeight / 4 + j * tileSize; //OFFSET THE Z AXIS SO THE TERRAIN FIST IN THE SCREEN NICELY
selectedTile.y = (int)placementCoord.z - gridHeight * CalculateMovementZ();
/*
placementCoord.x = selectedTile.x;
placementCoord.y = 10f;
placementCoord.z = selectedTile.y;
Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), placementCoord,
Quaternion.identity);
//*/
DeleteTile();
}
placementCoord.x += gridWidth * CalculateMovementX();
selectedTile.x = (int)placementCoord.x;
//LOOP TO SPAWN NEW TILES
for (j = 0; j < gridHeight; j++)
{
placementCoord.z = (int)Mathf.Floor(character.transform.position.z) - gridHeight / 4 + j * tileSize; //OFFSET THE Z AXIS SO THE TERRAIN FIST IN THE SCREEN NICELY
selectedTile.y = (int)placementCoord.z;
if (CalculateMovementX() > 0)
i = gridWidth;
else
i = 0;
GenerateTile();
}
}
if (Mathf.Abs(CalculateMovementZ()) > 0) //IF PLAYER MOVES VERTICALLY
{
if (CalculateMovementZ() < 0)
placementCoord.z = Mathf.Floor(character.transform.position.z) * tileSize - ((gridHeight / 4 * 3) + 1) * -1; // 3/4 TIMES TERRAIN SIZE;
else
placementCoord.z = Mathf.Floor(character.transform.position.z) * tileSize - (gridHeight / 4) - 1; // 1/4 TIMES TERRAIN SIZE
selectedTile.y = (int)placementCoord.z;
//LOOP TO DELETE OLD TILES
for (i = 0; i < gridWidth; i++)
{
placementCoord.x = Mathf.Floor(character.transform.position.x) - gridWidth / 2 + i * tileSize;
selectedTile.x = (int)placementCoord.x - gridWidth * CalculateMovementX();
/*
placementCoord.x = selectedTile.x;
placementCoord.y = 10f;
placementCoord.z = selectedTile.y;
Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), placementCoord,
Quaternion.identity);
//*/
DeleteTile();
}
if (CalculateMovementZ() < 0)
placementCoord.z -= gridHeight;
else
placementCoord.z += gridHeight;
selectedTile.y = (int)placementCoord.z;
//LOOP TO SPAWN NEW TILES
for (i = 0; i < gridWidth; i++)
{
placementCoord.x = (int)Mathf.Floor(character.transform.position.x) - gridWidth / 2 + i * tileSize; //OFFSET THE Z AXIS SO THE TERRAIN FIST IN THE SCREEN NICELY
selectedTile.x = (int)placementCoord.x;
if (CalculateMovementZ() > 0)
j = gridHeight;
else
j = 0;
GenerateTile();
}
}
previousPosition.x = Mathf.Floor(character.transform.position.x);
previousPosition.z = Mathf.Floor(character.transform.position.z);
}
This is an example of what happens…
The white blocks are the things that are being destroyed
The blue arrow is the direction I was moving
The red marked thing is what’s wrong: