I have a script that I found on Unity Answers that generates a galaxy of cubes. I tried out the script, but it proved too much work and unity crashed. I thought ok, it generates 1000 stars, so i'll lower all the values. I lowered stars, arms, radius and spread, but it still crashes. Eventually, I lost patience and set it to make 5 stars, 1 arm, 10 radius and 5 spread, but IT STILL CRASHES. I have no idea if it's my computer, but I have 4GB RAM and i've made games that do much more effort than generating 5 cubes, but don't even lag, let alone crash.
public int numberArms = 6;
public int numberStars = 1000;
public float galaxyRadius = 500f;
public int spread = 100;
float fHatRandom(float fRange)
{
float fArea = 4 * Mathf.Atan(6.0f);
float fP = fArea * Random.value;
return Mathf.Tan(fP / 4) * fRange / 6.0f;
}
float fLineRandom(float fRange)
{
float fArea = fRange * fRange / 2;
float fP = fArea * Random.value;
return fRange - Mathf.Sqrt(fRange * fRange - 2 * fP);
}
// Use this for initialization
void Start()
{
Random.seed = 100;
int starsPerArm = numberStars / numberArms;
float fAngularSpread = spread / numberArms;
float fArmAngle = (360 / numberArms);
for (int arm = 0; arm < numberArms; arm++)
{
for (int i = 0; i < starsPerArm; i++)
{
float fR = fHatRandom(galaxyRadius);
float fQ = fLineRandom(fAngularSpread);
float fK = 1;
//float fA = numberArms * (fArmAngle);
float fA = arm * fArmAngle;
float fX = fR * Mathf.Cos(Mathf.Deg2Rad * (fA + fR * fK + fQ));
float fY = fR * Mathf.Sin(Mathf.Deg2Rad * (fA + fR * fK + fQ));
Vector3 starPos = new Vector3(fX, fY, arm * 4);
Collider[] colliders = Physics.OverlapSphere(starPos, 15);
if (colliders.Length == 0)
{
GameObject star = GameObject.CreatePrimitive(PrimitiveType.Cube);
star.transform.position = starPos;
star.transform.parent = transform;
Debug.Log(starPos);
}
else
{
i--;//because they overlapped, we try again.
}
}
}
}
↧