- Home /
Random generation of random asteroids
Hi all,
I need to write a script to populate a scene with randomly generated asteroids for a 3rd person 3D game. This script should be able to choose from 6 models and add various components like rigidbody, collider and force. It should also assign random size/mass values and random force values (both within a range). I will reuse/modify this script to place power-ups in the scene.
I know basic C# scripting and I'm willing to learn. I use Unity2017/C# and know very little of Java.
Thanks for any insight/code.
Here's the code I've written to achieve this goal. I've been revising this post as I've added key elements. Namely, it now does assign random size, mass and force to the asteroids. The trick was apparently to make my instantiated prefabs also GameObjects and then to use arrays to modify components values of each clone.
The values chosen suit my project but you can change them for yours. If you see anything that could be improved, let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroides : $$anonymous$$onoBehaviour {
// Instantiates prefabs in boxed terrain.
public GameObject[] Asteroid;
private int nombreDobjets;
private int index;
private Rigidbody[] rb;
private ConstantForce[] cf;
void Start()
{
nombreDobjets = Random.Range(888, 1234);
Vector3 TailleTerrain = GameObject.Find("Terrain").GetComponent<Terrain>().terrainData.size;
rb = new Rigidbody[nombreDobjets];
cf = new ConstantForce[nombreDobjets];
for (int i = 0; i < nombreDobjets; i++)
{
index = Random.Range(0, 6);
Vector3 pos = new Vector3(Random.Range(0, TailleTerrain.x), Random.Range(0, TailleTerrain.y), Random.Range(0, TailleTerrain.z));
GameObject go = Instantiate(Asteroid[index], pos, Quaternion.identity) as GameObject;
go.AddComponent<Rigidbody>();
go.AddComponent<CapsuleCollider>();
go.AddComponent<ConstantForce>();
go.transform.localScale += new Vector3(Random.Range(0, 2f), Random.Range(0, 2f), Random.Range(0, 2f));
rb[i] = go.GetComponent<Rigidbody>();
cf[i] = go.GetComponent<ConstantForce>();
rb[i].mass = Random.Range(345, 1000);
rb[i].AddForce(Random.Range(0, 23), Random.Range(0, 23), 0, Force$$anonymous$$ode.Acceleration);
rb[i].AddTorque(Random.Range(0, 23), Random.Range(0, 23), Random.Range(0, 23), Force$$anonymous$$ode.Acceleration);
cf[i].force = new Vector3(0,188,0);
}
}
Answer by ShadyProductions · Dec 07, 2017 at 08:04 PM
Here is the basic concept:
using UnityEngine;
public class AsteroidHandler : MonoBehaviour
{
[SerializeField]
private GameObject[] AsteroidPrefabs;
void Start()
{
SpawnAsteroids(100);
}
private void SpawnAsteroids(int amount)
{
for (int i=0; i < amount; i++)
{
var pos = new Vector3(Random.Range(-150, 150), Random.Range(-150, 150), Random.Range(-150, 150));
var chosenAsteroid = AsteroidPrefabs[Random.Range(0, AsteroidPrefabs.Length)];
var asteroid = Instantiate(chosenAsteroid , pos, Quaternion.identity);
// You can still manipulate asteroid afterwards like .AddComponent etc
}
}
}
Worked very well. Whipped up a quick 'roid in Blender, dropped it and this script into Unity and voila.... asteroid field.
Answer by APSchmidt · Dec 06, 2017 at 09:24 PM
This tutorial will teach you how to do that: https://unity3d.com/learn/tutorials/s/space-shooter-tutorial
I've seen this tutorial and went thru the relevant section (Creating Hazards and Spawning Waves). It doesnt go as far I need but it had some useful code parts.