- Home /
Random generation of Asteroids
Hello,
i want to generate an Astroid field.
I want to place them at random places around a coordinate with a max and min amount of asteroids or something like that. I found the instantiate command but im not sure how i would make the center var and the random range from that. And how do i make it so the "SpawnLoop" only runs as long as i want asteroids to be generated.I have 5 diffrent Asteroid Prefabs that i want to randomly choose from. And is there a way to only spawn in places that are far enough away from other asteroids?
I know these are alot of questions but it would be very helpful if someone can explain and help me with those.
Thanks in advance.
Answer by robertbu · Feb 05, 2014 at 10:45 PM
The only harder part of your question is "to only spawn in places that are far enough away from other asteroids". One solution is to make all of your models closely fit inside a set size sphere (unit sphere is easiest). Then you can use Physics.CheckSphere() to check to see if a position is available. Here is a bit of code to demonstrate:
#pragma strict
var asteroids : GameObject[]; // Initialized in the inspector
private var origin = Vector3.zero;
var minSize = 0.2;
var maxSize = 1.5;
var minCount = 20;
var maxCount = 45;
var minDistance = 3.0;
var maxDistance = 15.0;
function Start () {
origin = transform.position;
GenerateAsteroids(Random.Range(minCount, maxCount));
}
function GenerateAsteroids (count : int) {
for (var i = 0; i < count; i++) {
var size = Random.Range(minSize, maxSize);
var prefab = asteroids[Random.Range(0, asteroids.Length)];
for (var j = 0; j < 100; j++) {
var pos = Random.insideUnitSphere * (minDistance + (maxDistance - minDistance) * Random.value);
pos += origin;
if (!Physics.CheckSphere(pos, size / 2.0)) {
break;
}
}
var go = Instantiate(prefab, pos, Random.rotation);
go.transform.localScale = Vector3(size, size, size);
}
}
In the inspector you need to initialize the 'asteroids' array with the prefabs for the asteroids.
The for() loop using 'j' repeatedly tries to place the object in a position that does not conflict using the CheckSphere() call. Note you can increase the 'radius' parameter to push things further apart as needed. Also note that it does not try forever. It tries 500 times. The reason is that certain parameters (too tight an area and/or too big of object), can result in no areas to place an object without overlapping. Without the limit on the number of tries, the app would hang. I don't know how many asteroids you will have, but it would be more efficient to keep your own list and do your own distance check rather than relying on CheckSphere(). Test your app for performance and see if you need to make the change.
The script works great i just tried it. Thanks. I dont have time today to look at the code but on the weekend i will look into it and try to understand it. Anyway thanks for the help.
Ok just a one more thing. I attached the script to an empty game object and i want to set the origin var to the game objects transform.postion but when i change it to var origin = transform.postion; it doesnt work. It still takes the input that is set in the inspector view. I checked that by adding a Debug.Log that displays the origin.
Your likely problem is you did this in Start():
var origin = transform.postion;
Ins$$anonymous$$d you need to do this:
origin = transform.postion;
Because you use 'var', you created a local variable that hides the instance variable at the top of the file.
I have done it the way you said but it still spawns around (0,0,0) even though the Debug.Log now shows the loc of the empty game object.
I just tested it, and it worked fine. Without seeing your code, I don't know where you have the issue. I edited the above script to what I tested. It now takes the 'origin' from the position of the game object the script is attached to.
Answer by morgan23 · Feb 05, 2014 at 10:46 PM
You can find something like that in a tutorial series on the website under learn /tutorials/ projects/project space shooter it's very good too for understanding basics.
Answer by Snownebula · Jul 30, 2014 at 11:29 PM
Anyone else getting there asteroids floating away? Is there a way to prevent them from just floating too far away?
They are probably floating away because their spawns intersect each other and they get pushed. Or do you mean because you are pushing them?
You could write a script that checks how far they are away from your worlds origin. Something like this (untested/pseudo-code):
var x = transform.position.x
var y = transform.position.y
var z = transform.position.z
var maxDistance = 100.0;
function Update (){
if (x>maxDistance || y>maxDistance ||z>maxDistance)
rigidbody.velocity = Vector3(0,0,0);
}
This would basically freeze the asteroid if it gets to far away from the origin.
I think its because there spawns intersect each other on start.
Your answer
Follow this Question
Related Questions
Spawning if there is space 1 Answer
How can I go about creating an "Infinate" asteroid field?? 1 Answer
Spawning enemies at an offset 1 Answer
Create a random group of asteroids. 2 Answers
How to to spawn player, according to id? 0 Answers