- Home /
random spawn locations....
What can I change or add to my script to have prefab objects randomly spawn from three locations. I don't need to put forward velocity to the objects spawned because they have AI script on the objects already. I just need them to be dropped from a point in space.
The Code:
var spawn : Transform[];
var game_cube : Rigidbody;
var cube_count = 0;
InvokeRepeating("LaunchProjectile", 2, 3);
function Update() {
if (cube_count>= 10) {
CancelInvoke();
}
}
function LaunchProjectile () {
var randomPick : int = Mathf.Abs(Random.Range(0,3));
instance = Instantiate(game_cube, spawn[randomPick].position, spawn[randomPick].rotation);
instance.velocity = transform.TransformDirection( Vector3( 0, 0, 0 ) );
cube_count = cube_count +1;
}
What's wrong? This code should be doing what you expect. The only weird thing is the $$anonymous$$athf.Abs function: you don't need it, since Random.Range(0, 3) returns a random number between 0 and 2 (the max limit never is reached in the int version of Random.Range).
Another thing: you can just do instance.velocity = Vector3.zero;, since the zero vector has no direction to transform.
I'm getting an error saying: IndexOutOfRangeException: Array index is out of range. SpawnController.LaunchProjectile () (at Assets/Standard Assets/SpawnController.js:19)
Answer by Martijn Hendriks · Oct 01, 2011 at 08:50 AM
I'm not good with javascript, but I think it has something to do with the initialisation of our spawn array. Are you sure that this array is created and filled correctly, since your declaration doesn't specify the size of the array an what is inside its entries? Have you set your spwan locations in the inspector?
Answer by SHG · May 17, 2015 at 06:36 PM
I wrote a random spawner script a while back, so here ya go!
var timer : float = 0;
var timeofwait : float = 0.25;
var spawning : boolean = false;
var prefab : GameObject;
var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : Transform;
var spawn4 : Transform;
var prefab1 : GameObject;
var prefab2 : GameObject;
var prefab3 : GameObject;
var prefab4 : GameObject;
var soundeffect: AudioClip;//spawn
function Update () {
//check if spawning at the moment, if not add to timer
if(!spawning){
timer += Time.deltaTime;
}
//when timer reaches 2 seconds, call Spawn function
if(timer >= 1){
Spawn();
GetComponent.<AudioSource>().clip = soundeffect;
GetComponent.<AudioSource>().volume = 1;
GetComponent.<AudioSource>().Play();
}
}
function Spawn(){
var randomvolume : float = Mathf.Abs(Random.Range(0.35,1));
//set spawning to true, to stop timer counting in the Update function
spawning = true;
//reset the timer to 0 so process can start over
timer = 0;
//select a random number, inside a maths function absolute command to ensure it is a whole number
var randomPick : int = Mathf.Abs(Random.Range(1,5));
var randomPrefab : int = Mathf.Abs(Random.Range(1,5));
//create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
var location : Transform;
//check what randomPick is, and select one of the 3 locations, based on that number
if(randomPick == 1){
location = spawn1;
prefab = prefab1;
}
else if(randomPick == 2){
location = spawn2;
prefab = prefab2;
}
else if(randomPick == 3){
location = spawn3;
prefab = prefab3;
}
else if(randomPick == 4){
location = spawn4;
prefab = prefab4;
}
//create the object at point of the location variable
var thingToMake = Instantiate(prefab, location.position, location.rotation);
//halt script for 1 second before returning to the start of the process
yield WaitForSeconds(timeofwait);
//set spawning back to false so timer may start again
spawning = false;
}