Question by
Augustus_Argento · Nov 29, 2021 at 05:32 PM ·
instantiaterandomspawnfield of viewasteroid
Trying to make a field of meteors forever, but I can tell I'm doing it horribly
What I'm trying to do is to make a field of asteroids, where there are a set of ten random grids that can spawn around a player-controlled rocket as it moves. A grid is a 4x4 square area where a random amount (between 1 and 4) of meteors spawn in random location.
I can successfully make one grid (a random amount of randomly placed meteors), but I'm having trouble storing the grids, and I have no idea how to spawn the grids around the rocket as it moves (I want to do this so the game doesn't try to spawn meteors literally everywhere, thus killing my computer).
Here's the code I have so far, as you can see it is very bad:
public class CloneMe : MonoBehaviour
{
// Start is called before the first frame update
public GameObject self;
public int i;
public bool cloneThis = true;
void Start()
{
duplicateMe();
}
public void duplicateMe()
{
// makes sure this only runs on the original meteor
if (cloneThis == true)
{
int amountOfClones = Random.Range(1, 3);
// makes a random amount of meteors in random positions
for (i = 0; i < amountOfClones; i++)
{
Vector2 firstPosition;
Vector2 secondPosition;
Vector2 thirdPosition;
Vector2 fourthPosition;
int differenceInXPosition;
if (amountOfClones == 1)
{
firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
transform.position = new Vector2(firstPosition);
var newObj = Instantiate(self);
newObj.GetComponent<CloneMe>().cloneThis = false;
differenceInXPosition = 0;
}
if (amountOfClones == 2)
{
firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
secondPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
transform.position = new Vector2(firstPosition);
var newObj = Instantiate(self);
transform.position = new Vector2(secondPosition);
newObj = Instantiate(self);
newObj.GetComponent<CloneMe>().cloneThis = false;
differenceInXPosition = System.Math.Abs(firstPosition.x - secondPosition.x);
}
if (amountOfClones == 3)
{
firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
secondPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
thirdPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
transform.position = new Vector2(firstPosition);
var newObj = Instantiate(self);
transform.position = new Vector2(secondPosition);
newObj = Instantiate(self);
transform.position = new Vector2(thirdPosition);
newObj = Instantiate(self);
newObj.GetComponent<CloneMe>().cloneThis = false;
differenceInXPosition = System.Math.Max(firstPosition.x, secondPosition.x, thirdPosition.x) - System.Math.Min(firstPosition.x, secondPosition.x, thirdPosition.x);
}
if (amountOfClones == 4)
{
firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
secondPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
thirdPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
fourthPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
transform.position = new Vector2(firstPosition);
var newObj = Instantiate(self);
transform.position = new Vector2(secondPosition);
newObj = Instantiate(self);
transform.position = new Vector2(thirdPosition);
newObj = Instantiate(self);
transform.position = new Vector2(fourthPosition);
newObj = Instantiate(self);
newObj.GetComponent<CloneMe>().cloneThis = false;
differenceInXPosition = System.Math.Max(firstPosition.x, secondPosition.x, thirdPosition.x, fourthPosition.x) - System.Math.Min(firstPosition.x, secondPosition.x, thirdPosition.x, fourthPosition.x);
}
}
}
cloneThis = false;
}
// Update is called once per frame
void Update()
{
}
}
The code for one grid is as follows:
public class CloneMe : MonoBehaviour
{
// Start is called before the first frame update
public GameObject self;
public int i;
public bool cloneThis = true;
void Start()
{
duplicateMe();
}
public void duplicateMe()
{
// makes sure this only runs on the original meteor
if (cloneThis == true)
{
int amountOfClones = Random.Range(1, 3);
// makes a random amount of meteors in random positions
for (i = 0; i < amountOfClones; i++)
{
transform.position = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
var newObj = Instantiate(self);
newObj.GetComponent<CloneMe>().cloneThis = false;
}
}
cloneThis = false;
}
// Update is called once per frame
void Update()
{
}
}
as you can probably guess my head hurts so simple words please and thank you
Comment