So basically I got two prefabs (Cube 1 and Cube 2 e.g.) and I want them to be generated in random amounts connected together having the option to make the Cube 2 or Cube 1 “rarer” on some occasions.
Screenshot as example.
using UnityEngine;
public class CubeScript : MonoBehaviour {
private int step = 0;
//How many objects do you want to make?
public int steps = 3;
//Odds of second cube being spawned... 3 = 0/3 odds, 1 = no second cube
public int rarity = 3;
//Last position is saved to connect cubes
Vector3 LastPosition = Vector3.zero;
//Used as a cache
private GameObject CurrentObject;
public GameObject Cube1Prefab;
public GameObject Cube2Prefab;
void Update()
{
if (step < steps)
{
NextStep();
step++;
}
}
private void NextStep()
{
if (Random.Range(0, rarity) == 1)
{
CurrentObject = Instantiate(Cube2Prefab, LastPosition + GetRandomDirection(), Quaternion.identity);
}
else
{
CurrentObject = Instantiate(Cube1Prefab, LastPosition + GetRandomDirection(), Quaternion.identity);
}
LastPosition = CurrentObject.transform.position;
}
private Vector3 GetRandomDirection()
{
switch (Random.Range(0, 2))
{
case 0: return new Vector3(1, 0, 0); //X
case 1: return new Vector3(0, 0, 1); //Z
}
return Vector3.zero;
}
}
Thats not what I quite meant. I was talking about chunks or asteroids of cubes in random amounts and sizes of these “asteroids” to spawn around.
You should really appreciate, that someone gave any script.
You may need write your own, for such specific request.
Right now I am using this script for creating prefab asteroids but I want to use random sized asteroids instead of prefabs.
using UnityEngine;
/// <summary>
/// Place on a gameObject in your scene
/// </summary>
public class AsteroidGen : MonoBehaviour
{
[SerializeField] GameObject asteroidPrefab; // Link your cube prefab or whatever you want
[SerializeField] int asteroidCount; // The amount to spawn
[SerializeField] int radius = 100; // Radius of where they will spawn, center is (0,0,0)
void Start()
{
CreateAsteroids();
}
void CreateAsteroids()
{
for (int i = 0; i < asteroidCount; i++)
{
Vector3 randomPosition = Random.insideUnitSphere * radius;
Quaternion randomRotation = Random.rotation;
Instantiate(asteroidPrefab, randomPosition, transform.rotation);
//Vector3 randomScale = new Vector3(Random.Range(0.5f, 2f), Random.Range(0.5f, 2f), Random.Range(0.5f, 2f));
//go.transform.localScale = randomScale;
}
}
}
I have no idea how to dynamically create different asteroids if that’s what your asking, but if you do have a list of asteroid prefabs you can run through them and pick one out randomly from the folder and spawn it. So assuming your doing this, Ive added some physics to them and removed your range function, itll instead use the active terrain to spawn them all over it.
using UnityEngine;
public class AsteroidGenerator : MonoBehaviour {
[SerializeField] GameObject asteroidPrefab; // Link your cube prefab or whatever you want
[SerializeField] int asteroidCount; // The amount to spawn
[SerializeField] int minHeight = 1;
[SerializeField] int maxHeight = 250;
private Vector3 terrainPosition = Vector3.zero; //Cache terrain position
private GameObject currentAsteroid = null; //Cache each new asteroid before changing it
void Start()
{
terrainPosition = Terrain.activeTerrain.transform.position;
CreateAsteroids();
}
void CreateAsteroids()
{
for (int i = 0; i < asteroidCount; i++)
{
currentAsteroid = Instantiate(asteroidPrefab, GetRandomSpotOnTerrain(), Random.rotation);
}
}
private Vector3 GetRandomSpotOnTerrain()
{
return new Vector3(Random.Range(-terrainPosition.x, terrainPosition.x), Random.Range(minHeight, maxHeight), Random.Range(-terrainPosition.z, terrainPosition.z));
}
}
This is placed on the asteroid prefab that gets spawned in.
using UnityEngine;
public class AsteroidScript : MonoBehaviour {
public float Speed = 10;
private Rigidbody asteroidRb;
private void Start()
{
asteroidRb = gameObject.AddComponent<Rigidbody>();
asteroidRb.useGravity = false;
transform.rotation = Random.rotation;
transform.localScale = GetRandomScale();
asteroidRb.velocity = GetRandomDirection() * Speed;
}
private Vector3 GetRandomDirection()
{
switch (Random.Range(0, 3))
{
case 0: return new Vector3(1, 0, 0); //X
case 1: return new Vector3(0, 1, 0); //Y
case 2: return new Vector3(0, 0, 1); //Z
}
return Vector3.zero;
}
private Vector3 GetRandomScale()
{
return new Vector3(Random.Range(0.5f, 2f), Random.Range(0.5f, 2f), Random.Range(0.5f, 2f));
}
}
Hope this helps a bit!
There’s tons of procedural geometry stuff out on the net for Unity.
If you want to see some minor package I put together, check this:
Thanks a lot for the effort, but it wasn’t what I meant again tho. The so called “asteroids” are static objects consisting out of individual cubes (Reference to Minecraft). The Terrain function is quite useful to me, thanks for that but it’s still not what I am planning on doing.
My currently used prefab for the asteroids made out of two main components: “Rock Cube” and an “Ore Cube” (inside of that.)

But I want to generate huge “asteroids” that are made out of hundreds of blocks and them should be spawning infinitely on the whole terrain. The script that I was using was not perfect because a) it was using this radius function and b) it was using the prefab and that is not what I am planning to do out of it.
Adding a prefab to the prefab in every direction and having a changce that it continues adding them or stops and leaves the prefabs how they are and continues on to the next prefab. How do I do that in code
