2d Obstacle spawner with fixed distance between objects
I have a ball moving forward, and so far a single cube controlled by the mouse, I want to have at least 5 cubes on screen once the game starts with a certain range of space between them and have them have different spawn locations every time to kinda randomize the game. Whenever I seem to spawn though, it spawns on top of the already existing cube
No one would be able to help without seeing your code
using UnityEngine;
public class RandomSpawner : $$anonymous$$onoBehaviour { public GameObject prefab1;
public int platform;
// Use this for initialization
void Start()
{
InvokeRepeating("CreateObstacle", 1f, 1.5f); //generate
}
void CreateObstacle()
{
platform = Random.Range (1, 7); //radom number generator b/w 1 and 7
float randomY = Random.Range(-5f, 5f); // appear b/w -5 and 5 in y-axis
float rightScreenBound = 30; // spawn this much right of the screen
switch (platform)
{
case 1:
Instantiate(prefab1, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
break;
}
}
}
Ok so you only have a random y position so they will obviously spawn at the same x and z (which means on top of each other). You need to do the same random thing for x or z which ever way you are setup !!
How could i have missed that :p. Anyway, how could I make it spawn within a certain y axis?
Your answer
Follow this Question
Related Questions
Animation window time bar shows 1 minute increments - HELP 0 Answers
Get angle between a position and facing direction 0 Answers
Weird 2D rotation in child to parent relation hierarchy 0 Answers
Creating a background to have a random shape destroy effect 0 Answers
Exclude UI / HUD layer from post processing in Universal RP using 2D Renderer 0 Answers