I have been thinking about this problem for a while but I can’t manage to find where my algorithm is wrong :
I’m working on a procedural generated space game and I want the world to be composed of tiles that would have different functions, the problem is that I want them to be spaced so that the player can go between them, so I made so that they won’t spawn next to each other but when I try to add tiles that are empty, the algorithm is kind acting weird :
Here is my code :
using UnityEngine;
using System.Collections;
public class ChunkManagerS : MonoBehaviour
{
public int chunkLength;
public int spaceRange;
public int randomness;
public GameObject [,] primarChunk;
public GameObject platformPrefab = null;
void Start ()
{
primarChunk = new GameObject[chunkLength,chunkLength];
for(int x = 0; x < chunkLength; x++)
{
for(int z = 0; z < chunkLength; z++)
{
bool c = false;
for(int a = x - spaceRange; a <= x + spaceRange; a++)
{
for(int b = z - spaceRange; b <= z + spaceRange; b++)
{
if(a >= 0 && b >= 0 && a <= x && b <= z)
{
if(primarChunk[a,b] != null)
c = true;
}
}
}
if(!c)
{
//if(Random.Range(0,100) < randomness) Randomness system added later
//{
primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x, 0, z), Quaternion.identity);
//}
}
}
}
}
}
So everything is working well when I don’t use randomness, for example if I ask for a 100 tiles chunk with a spaceRange of 2 I get this result :
But if I had add the fact that a tile has 50% of chance to be empty, strange stuff happens :
Some tiles’ corners start colliding with other tiles’ corner and it’s not the result that I was looking for so if you have any ideas to resolve this problem I would really appreciate ^^.
Thx in advance !