x


Random Spawning of Objects at Random Locations

Hello, I have thrown together a random spawning script, however i would like it to be a bit more complicated. At the moment i have a spawn point being chosen at random however, that the chose spawn point will only ever spawn the same object. Here is what i have so far:

var timer : float = 0.0;
var spawning : boolean = false;
var meteor1 : Rigidbody;
var meteor2: Rigidbody;
var meteor3 : Rigidbody;

var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : Transform;

function Update () {

 if(!spawning){
  timer += Time.deltaTime;
 }

 if(timer >= 1.5){
  Spawn();
 }
}

function Spawn(){

 spawning = true;
 timer = 0;

 var randomPick : int = Mathf.Abs(Random.Range(1,4));
 var location : Transform;
 var whichObj : Rigidbody;

 if(randomPick == 1){
  location = spawn1;
  whichObj = meteor1;
  Debug.Log("Chose pos 1");
 }
 else if(randomPick == 2){
  location = spawn2;
  whichObj = meteor2;
  Debug.Log("Chose pos 2");
 }
 else if(randomPick == 3){
  location = spawn3;
  whichObj = meteor3;
  Debug.Log("Chose pos 3");
 }


 var meteorToMake : Rigidbody = Instantiate(whichObj, location.position, location.rotation);
  meteorToMake.AddForce(-Vector3(0,0,500));

 yield WaitForSeconds(1);
 spawning = false;
}

What I would like is to also choose the object that spawns from the spawn point randomly... Any ideas?

more ▼

asked Apr 27 '12 at 05:03 PM

TheFrankman123 gravatar image

TheFrankman123
162 24 35 37

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Try this:

var timer : float = 0.0;
var spawning : boolean = false;
var meteors : Rigidbody[];

var spawnPoints : Transform[];

function Update () {

 if(!spawning){
  timer += Time.deltaTime;
 }

 if(timer >= 1.5){
  Spawn();
 }
}

function Spawn(){

 spawning = true;
 timer = 0;

 var randomSpawnPick : int = Mathf.Abs(Random.Range(0,spawnPoints.length-1));
 var randomRigidPick : int = Mathf.Abs(Random.Range(0,meteors.length-1));
 var location : Transform;
 var whichObj : Rigidbody;

  location = spawnPoints[randomSpawnPick];
  whichObj = meteors[randomRigidPick];

 var meteorToMake : Rigidbody = Instantiate(whichObj, location.position, location.rotation);
  meteorToMake.AddForce(-Vector3(0,0,500));

 yield WaitForSeconds(1);
 spawning = false;
}
more ▼

answered Apr 27 '12 at 06:08 PM

bompi88 gravatar image

bompi88
741 2 5

what's all this about 'var thisG : GameObject; var enemies : GameObject[]; var closestEnemy : GameObject;'

Apr 27 '12 at 06:41 PM TheFrankman123

Sorry, you can delete those.. I pasted it in an old file, for editing. Some of the old stuff came along.. Are you sure you don´t want any enemies in your game :-P hehe..

Apr 27 '12 at 11:41 PM bompi88

Haha, i have plenty of enemies but no random spawning required. Was just wondering if i was missing something.

May 02 '12 at 04:09 PM TheFrankman123
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x575
x441
x110

asked: Apr 27 '12 at 05:03 PM

Seen: 1550 times

Last Updated: May 02 '12 at 04:09 PM