I currently am learning C# and Unity and was wondering how I could generate a procedural galaxy. I already wrote a code for a randomized Asteroid structure in a certain field, however, I can’t figure out how to focus on an area - so to speak the core, so I have more asteroids/objects at the centre than at the edge - and how I could simulate the spirals of a galaxy…
Here is my current generator-script.
If anybody has suggestions or ideas, I would be happy to hear them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
public int maxAsteroids;
public float tumble;
public GameObject spherePrefab;
public GameObject asteroid1;
public GameObject asteroid2;
public GameObject asteroid3;
public float asteroidXMin, asteroidXMax, asteroidYMin, asteroidYMax, asteroidZMin, asteroidZMax;
public int xMin, xMax, yMin, yMax, zMin, zMax;
Vector3 pos;
GameObject[] asteroids;
float x;
float y;
float z;
void Start () {
GameObject[] asteroids = new GameObject[] { asteroid1, asteroid2, asteroid3 };
AsteroidGeneration(asteroids[Random.Range(1, asteroids.Length)]);
}
void AsteroidGeneration(GameObject theObject)
{
StartCoroutine(Asteroid(theObject));
}
IEnumerator Asteroid(GameObject theObject)
{
for (int i = 0; i <= maxAsteroids; i+=1)
{
Debug.Log(i);
x = Random.Range(xMin, xMax);
z = Random.Range(zMin, zMax);
y = Random.Range(yMin, yMax);
pos = new Vector3(x, y, z);
if (Physics.CheckSphere(pos, asteroidXMin))
{
i-=1;
Debug.Log("Game Object already there " + i);
}
else
{
Quaternion rot = new Quaternion(0, 0, 0, 0);
transform.position = pos;
Instantiate(theObject, transform.position, rot);
}
yield return new WaitForSeconds(0.01f);
}
}
void Update () {
}
}

