- Home /
Procedural Spawning Theorems
Hey guys, simple task here that I would like a bit of fine-tuning.
So basically I'm trying to re-make this game called Cube Runner and publish it for Android because I can't find any good versions of the game and I thought I'd make my own ;).
The link is at http://cuberunnergame.com/
I have mostly the entire game set up, but I have a pretty crappy cube spawn system. I use the following code to spawn the cubes:
void SpawnObstacles () {
int r = Random.Range (0, Obstacles.Length);
GameObject obstacle1 = Instantiate (Obstacles[r], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
obstacle1.name = Obstacles[r].name;
obstacle1.transform.localScale *= Random.Range (.8f, 1.2f);
int m = Random.Range (0, Obstacles.Length);
GameObject obstacle2 = Instantiate (Obstacles[m], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
obstacle2.name = Obstacles[m].name;
obstacle2.transform.localScale *= Random.Range (.8f, 1.2f);
int v = Random.Range (0, Obstacles.Length);
GameObject obstacle3 = Instantiate (Obstacles[v], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
obstacle3.name = Obstacles[v].name;
obstacle3.transform.localScale *= Random.Range (.8f, 1.2f);
int b = Random.Range (0, Obstacles.Length);
GameObject obstacle4 = Instantiate (Obstacles[b], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
obstacle4.name = Obstacles[b].name;
obstacle4.transform.localScale *= Random.Range (.8f, 1.2f);
}
Basically, I have an array of obstacles that are spawned randomly and with random sizes in random positions in front of the player. The problem is that some of the cubes spawn on top of each other, some of them are also sometimes too close together, and many times I get an impossible set-up. In the original cube runner, the cubes are spawned slightly spread out and the game is almost never completely impossible.
Does anyone have a better algorithm for spawning cubes? Or an idea on how I would implement it?
Thanks in advance, Aman Jha
All this could be done in a for loop.
void SpawnObstacles ()
{
int r;
float posX;
float scale;
GameObject obstacle;
for ( int i = 0; i < 4; i ++ )
{
r = Random.Range (0, Obstacles.Length);
posX = Random.Range (transform.position.x - 150f, transform.position.x + 150f);
scale = Random.Range (.8f, 1.2f);
obstacle = Instantiate (Obstacles[r], new Vector3(posX, 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
obstacle.name = Obstacles[r].name;
obstacle.transform.localScale *= scale;
}
}
edit : there is a thread in the forums that you may find interesting : http://forum.unity3d.com/threads/how-to-randomly-instantiate-cubes-that-dont-overlap.263161/#post-1755624
Thanks for the for loop idea *facepalm. I was speed coding this app, everything was to be done in a day and I was kinda rushing. Thanks for the link too, I'll check it out!!
Your answer
Follow this Question
Related Questions
Spawn object problem 1 Answer
screenshot 0 Answers
How To Call Health Script From Another Script ? 1 Answer
Android Development; How to use SDK manager! 1 Answer
Unity Scale One Text Field GUI 2 Answers