Help with a simple Random Generation Script

Hello,

Right now my random generation does this :

The script is :

    #pragma strict

     

    var spawnPositionX : float;
    var spawnPositionY : float;

    var cloud : GameObject;
    var blackcloud : GameObject;
    var graycloud : GameObject;
    var greencloud : GameObject;
    var yellowcloud : GameObject;
    var orangecloud : GameObject;
    var purplecloud : GameObject;
    var bluecloud : GameObject;
    var redcloud : GameObject;
    var rainbowcloud : GameObject;
    var pinkcloud : GameObject;

    var go = new GameObject[38];
    var rainbowrandom = new GameObject[66];

function Start(){
	spawnPositionX = Mathf.RoundToInt (transform.position.x + 4);

	go[1] = graycloud;
	go[2] = greencloud;
	go[3] = yellowcloud;
	go[4] = orangecloud;
	go[5] = purplecloud;
	go[6] = bluecloud;
	go[7] = redcloud;
	go[8] = rainbowcloud;
	go[9] = blackcloud;
	go[10] = pinkcloud;
	go[11] = pinkcloud;
	go[12] = yellowcloud;
	go[13] = graycloud;
	go[14] = redcloud;
	go[15] = redcloud;
	go[16] = purplecloud;
	go[17] = purplecloud;
	go[18] = purplecloud;
	go[19] = orangecloud;
	go[20] = orangecloud;
	go[21] = orangecloud;
	go[22] = orangecloud;
	go[23] = cloud;
	go[24] = cloud;
	go[25] = cloud;
	go[26] = cloud;
	go[27] = cloud;
	go[28] = cloud;
	go[29] = cloud;
	go[30] = cloud;
	go[31] = cloud;
	go[32] = cloud;
	go[33] = cloud;
	go[34] = cloud;
	go[35] = cloud;
	go[36] = cloud;
	go[37] = cloud;
	go[38] = cloud;
	
	rainbowrandom[1] = blackcloud;
	rainbowrandom[2] = blackcloud;
	rainbowrandom[3] = graycloud;
	rainbowrandom[4] = graycloud;
	rainbowrandom[5] = graycloud;
	rainbowrandom[6] = rainbowcloud;
	rainbowrandom[7] = greencloud;
	rainbowrandom[8] = greencloud;
	rainbowrandom[9] = greencloud;
	rainbowrandom[10] = greencloud;
	rainbowrandom[11] = bluecloud;
	rainbowrandom[12] = bluecloud;
	rainbowrandom[13] = bluecloud;
	rainbowrandom[14] = bluecloud;
	rainbowrandom[15] = pinkcloud;
	rainbowrandom[16] = pinkcloud;
	rainbowrandom[17] = pinkcloud;
	rainbowrandom[18] = pinkcloud;
	rainbowrandom[19] = pinkcloud;
	rainbowrandom[20] = yellowcloud;
	rainbowrandom[21] = yellowcloud;
	rainbowrandom[22] = yellowcloud;
	rainbowrandom[23] = yellowcloud;
	rainbowrandom[24] = yellowcloud;
	rainbowrandom[25] = yellowcloud;
	rainbowrandom[26] = redcloud;
	rainbowrandom[27] = redcloud;
	rainbowrandom[28] = redcloud;
	rainbowrandom[29] = redcloud;
	rainbowrandom[30] = redcloud;
	rainbowrandom[31] = redcloud;
	rainbowrandom[32] = redcloud;
	rainbowrandom[33] = redcloud;
	rainbowrandom[34] = blackcloud;
	rainbowrandom[35] = graycloud;
	rainbowrandom[36] = purplecloud;
	rainbowrandom[37] = purplecloud;
	rainbowrandom[38] = purplecloud;
	rainbowrandom[39] = purplecloud;
	rainbowrandom[40] = purplecloud;
	rainbowrandom[41] = purplecloud;
	rainbowrandom[42] = purplecloud;
	rainbowrandom[43] = purplecloud;
	rainbowrandom[44] = orangecloud;
	rainbowrandom[45] = orangecloud;
	rainbowrandom[46] = orangecloud;
	rainbowrandom[47] = orangecloud;
	rainbowrandom[48] = orangecloud;
	rainbowrandom[49] = orangecloud;
	rainbowrandom[50] = orangecloud;
	rainbowrandom[51] = orangecloud;
	rainbowrandom[52] = orangecloud;
	rainbowrandom[53] = cloud;
	rainbowrandom[54] = cloud;
	rainbowrandom[55] = cloud;
	rainbowrandom[56] = cloud;
	rainbowrandom[57] = cloud;
	rainbowrandom[58] = cloud;
	rainbowrandom[59] = cloud;
	rainbowrandom[60] = cloud;
	rainbowrandom[61] = cloud;
	rainbowrandom[62] = cloud;
	rainbowrandom[63] = cloud;
	rainbowrandom[64] = cloud;
	rainbowrandom[65] = cloud;
	rainbowrandom[66] = cloud;
}

function Update(){
	CreateCloud();
}
	
function OnCollisionEnter(myCol : Collision){
		
	if(myCol.gameObject.tag == "fallout"){
		spawnPositionX = 0;
		spawnPositionY = 0;
	}
}

function OnTriggerEnter(myCol : Collider){
	
	if(myCol.gameObject.tag == "rainbowcloud"){
		var rainbowjackpot = Random.Range(0,66);
		Instantiate(rainbowrandom[rainbowjackpot], myCol.transform.parent.gameObject.transform.position,Quaternion.Euler(0,0,180));
		Destroy(myCol.transform.parent.gameObject);
		if(rainbowjackpot == 5){
			Counter.score += 1500;
		}
	}
}

function CreateCloud(){
	if (spawnPositionX < transform.position.x + 15)
	{
		spawnPositionX += Random.Range(0.5102,4.0142);
		spawnPositionY = Random.Range(-1,5);
		
		Instantiate(go[Random.Range(0,39)],new Vector3(spawnPositionX,spawnPositionY,0),Quaternion.Euler(0,0,180));
}
}

I know there are probably easier way to do the arrays but it suits my needs and it is something I understand so please just focus on fixing my random generation problem. Thank you in advance.

I would like to have the clouds spawn like this (separate from eachother just enough to jump to but I dont want them so they are so compact that the game is very easy to play) :

I have already tried to make the minimum x value much higher but then it becomes a cloud to be spawned every few meters. Instead I want it so 2 clouds can spawn with a very close x value but the y value can be different. I was thinking of implementing a distance between the 2 clouds and if it is less than it then it doesn’t get spawned. I just don’t know how to make that happen since I am not the best coder out there.

Still need help.

Please I could really just use a simple explanation.

Bump

Can someone please give me a short explanation?

Why don’t you randomly choose a distance, then randomly choose a direction? You can limit the direction to the right hemisphere at least mostly right hemisphere

You could have your Y direction be reliant on your X direction, something like

float rnd = Random.Range(0.5, 4);
spawnPositionX += rnd;
spawnPositionY = 5 - rnd;

this way if they are close together horizontally they will be far vertically, and if they are close vertically they are far horizontally.

Greynz the only thing that does is if the x value is close to the same as the last one then the y value will be almost the same as the last y value. Somehow I need to save the values from the last CloudCreate.

Bump again. I want the random generation to be something like doodle jump. I want it to be horizontal instead of vertical and have them spaced apart but close enough together to be able to jump to. Also there cannot be any impossible jump.

Still need help

You said you somehow need to “save the values from the last CloudCreate.” It seems to me you already are, in the spawnPositionX and spawnPositionY variables.

So use Mathf.Max() and Mathf.Min() to limit the parameters you’re passing to Random Range() For example
Random.Range(-1,Mathf.Min(5,spawnPositionY+2))
this would limit the new cloud to being 2 units higher than the last one.

Try this:

if(spawnPosition.y < 2)
    spawnPosition.y = Random.Range(2,5);
else
    spawnPosition.y = Random.Range(-1, 2);

Rrh im pretty sure the spawnpositionx and y are for the current spawn not the spawn before.

Also makeshiftwing that would make it so when its close it spawns it at a higher y pos and if its far away then it spawns lower

here`s my work around that I use , this will randomly spawn an object within coordinates
when it reaches a certain point , it can also spawn if something is destroyed etc etc

public float enemySpeed;

void Update () 
	{
		transform.Translate (Vector3.left * Speed * Time.deltaTime);
		
		if (transform.position.x >= 1.617505f)
		{
			float randomY = Random.Range (-0.4122618f ,0.7854744f);
			transform.position = new Vector2 (-1.646172f, randomY);
		}
	}

destroy the object onTriggerEnter for example and spawn it

float randomZ = Random.Range (-1.09f, 0f);
transform.position = new Vector3 (9f, 2f, randomZ);

Destroy (gameObject);

[/CODE]

All i need is a code that can spawn them far apart and cant really be close together

I still need help please

But they aren’t local variables defined only in the Update function. So the next time Update is called, they will still contain the values they were assigned last time.

I misunderstood what your goal was. My solution was for if you wanted to ensure they were always close enough that you could jump from one to the other. I didn’t notice your objection that it can’t be too close.

If you think it’s necessary, you can make an extra 2 variables called lastpositionx and y, and assign them the value of spawnpositionx and y at the end of creating a cloud.

Or another approach you could make nextpositonx and y, assign them your random values, then while they are either too close or too far, assign them new random values. Once they are good values, you assign spawnpositionx and y to those and continue.

Or another idea is you could make a Vector2 which is the relative distance from the last cloud and assign the .x and .y values. Then you could use the .magnitude and .normalize to set the minimum and maximum distance. Then change the spawnpositionx and y by adding the .x and .y values from the Vector2.