Hi,I was following the tutorial abut the flappy bird game but when I start the game ,it spawns tro columns one near another sometimes there are overlaid .How fix it?
up
Show the code you’re using to spawn the columns. Use code tags.
using UnityEngine;
using System.Collections;
public class SpawnColonne : MonoBehaviour {
public GameObject ColonnaP; //Prefab Colonna
public int NumeroColonne = 3; //Colonne in attesa
public float spawnRate = 3f; //VelocitĂ spawn
public float colonneMin = -1f; //Valore minimo poszione colonna
public float colonneMax = 3.5f; //Valore massimo poszione colonna
GameObject[] colonne; //collection of pooled columns
int colonnaCorrente = 0; //indice colonna
void Start()
{
//Colonna sono
colonne= new GameObject[NumeroColonne];
//loop through the collection and create the individual columnsper creare le colonne
for(int i = 0; i < NumeroColonne; i++)
{
//Creiamo
colonne[i] = (GameObject)Instantiate(ColonnaP);
}
//Si spawna
StartCoroutine ("SpawnLoop");
}
public void StopSpawn()
{
//Stoppiamo lo spawn
StopCoroutine("SpawnLoop");
}
IEnumerator SpawnLoop()
{
//infinite loop: use with caution
while (true)
{
//Recuperiamo la psozione per lo spawn
Vector3 pos = transform.position;
//..impostiamo a random y position...
pos.y = Random.Range(colonneMin, colonneMax);
//...then set the current column to that position.
colonne[colonnaCorrente].transform.position = pos;
//Aumentiamo le colonne
if(++colonnaCorrente >= NumeroColonne)
colonnaCorrente = 0;
//lasciamo questa coroutine fino a quando il tempo scade
yield return new WaitForSeconds(spawnRate);
}
}
}
You currently have a randomized Y position, but there’s nothing about X position. As far as I can tell it’s spawning them in the current X position of the gameobject. You could add a min and max for the X position, and add that to the “pos.x” before setting the column position. That way each column will be randomly spaced. The minimum X would be at least the width of one column.
Sorry this isn’t my code I took from unity and I tried to learn something could you show me what I ve to add?
I’m not entirely certain if this will work but something like this. You can configure the values in the inspector:
using UnityEngine;
using System.Collections;
public class SpawnColonne : MonoBehaviour {
public GameObject ColonnaP; //Prefab Colonna
public int NumeroColonne = 3; //Colonne in attesa
public float spawnRate = 3f; //VelocitĂ spawn
public float colonneMinX = 3f;
public float colonneMaxX = 5f;
public float colonneMinY = -1f; //Valore minimo poszione colonna
public float colonneMaxY = 3.5f; //Valore massimo poszione colonna
GameObject[] colonne; //collection of pooled columns
int colonnaCorrente = 0; //indice colonna
void Start() {
//Colonna sono
colonne = new GameObject[NumeroColonne];
//loop through the collection and create the individual columnsper creare le colonne
for(int i = 0; i < NumeroColonne; i++) {
//Creiamo
colonne[i] = (GameObject)Instantiate(ColonnaP);
}
//Si spawna
StartCoroutine("SpawnLoop");
}
public void StopSpawn() {
//Stoppiamo lo spawn
StopCoroutine("SpawnLoop");
}
IEnumerator SpawnLoop() {
//infinite loop: use with caution
while(true) {
//Recuperiamo la psozione per lo spawn
Vector3 pos = transform.position;
pos.x += Random.Range(colonneMinX, colonneMaxX);
//..impostiamo a random y position...
pos.y = Random.Range(colonneMinY, colonneMaxY);
//...then set the current column to that position.
colonne[colonnaCorrente].transform.position = pos;
//Aumentiamo le colonne
if(++colonnaCorrente >= NumeroColonne)
colonnaCorrente = 0;
//lasciamo questa coroutine fino a quando il tempo scade
yield return new WaitForSeconds(spawnRate);
}
}
}
It seems not work…
How is it not working? Do you get errors? What happens?
Where exactly is this “SpawnColonne” gameobject? Is it off screen to the right or something? The spawn locations are relative to the location of that object, so you should be able to play around with the values in the inspector to get it to work correctly.
Well no errors but now it creates 5 columns overlaid it’s put of camera range is a simple game object woth a script attached to it
Have you tried increasing the values of “colonneMinX” and “colonneMaxX” to space them out further?
Yes I have but the value colonna max should be the value after which is placed the next column
If you increase the colonneMinX, then the columns should spawn at least that distance apart or further if colonneMaxX is even greater.
Well why so there is an overlay problem?
In the inspector, what did you set the minX and maxX to? Try setting the min to 10 and max to 12, do you see a difference?
You’re not really giving me enough information to know what the problem is.
Thanks for your time first,Yesterday I hadn’t enough time to do interesting stuff Here the spawn object the birs position is x -7.64 y 0.35
So is this working for you now, or do you still have problems?
no nothing has changed
So changing MinX and MaxX is having no effect on the position of the columns?
no effect there is always the problem