- Home /
Question by
S4lad · Dec 09, 2018 at 05:15 PM ·
generationprocedural-generation
Generating Cube composed Asteroids in Space
I want to generate random sized Asteroids composed out of Cubes in space but I have no clue how to do that.
Screenshot from a game as example
unbenannt.png
(200.0 kB)
Comment
Best Answer
Answer by Vega4Life · Dec 09, 2018 at 05:50 PM
Here is a simple script to spawn some asteroids around.
using UnityEngine;
/// <summary>
/// Place on a gameObject in your scene
/// </summary>
public class SpawnAsteroids : MonoBehaviour
{
[SerializeField] GameObject asteroidPrefab; // Link your cube prefab or whatever you want
[SerializeField] int asteroidCount; // The amount to spawn
[SerializeField] float radius = 100f; // 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;
GameObject go = Instantiate(asteroidPrefab, randomPosition, randomRotation);
Vector3 randomScale = new Vector3(Random.Range(0.5f, 2f), Random.Range(0.5f, 2f), Random.Range(0.5f, 2f));
go.transform.localScale = randomScale;
}
}
}
Thanks a lot! I was searching for a script like this for ages...
Is there a script to generate random amounts of cubes in random positions, ins$$anonymous$$d of using prefabs?