What you need to do is assign the random positions the monsters could spawn in an array:
var randomPositions: Transform[];
This will give you a dropdown list in the inspector where you can put as many random positions as you want. Next you want to call the function that will spawn the monsters:
if(Input.GetButtonDown("Jump")){
//Spawn monster code
}
So if I hit space it will run the spawn monster code. Next is another variable I need to set up; how many monsters I want spawned:
var numberOfMonstersToSpawn: int;
Next in your monster code, you want to run a while loop to run the monster code as many times as you need to spawn all the monsters:
var i: int = 0;
while(i < numberOfMonstersToSpawn){
//Code here
i++;
}
So what that code will do is run a loop for however monsters you want spawned. So if you want 3 monsters spawned, it will run the loop 3 times before stopping execution. Next is the actual spawning code. First we’re going to have it pick a random spot to spawn the monster in:
var spawnIndex: int = Random.Range(0, randomPositions.Length);
This basically chooses a number between 0 and the number of random positions you have. Next we’ll have the code for the instantiation:
Instantiate(monster, randomPositions[spawnIndex].position, randomPositions[spawnIndex].rotation);
And there you go. It should spawn the monsters at a random position for as many monsters as you like. Here’s the complete code for reference:
var monster: GameObject;
var randomPositions: Transform[];
var numberOfMonstersToSpawn: int = 0;
function Update () {
if(Input.GetButtonDown("Jump")){
var i: int = 0;
while(i < numberOfMonstersToSpawn){
var spawnIndex: int = Random.Range(0, randomPositions.Length);
Instantiate(monster, randomPositions[spawnIndex].position, randomPositions[spawnIndex].rotation);
i++;
}
}
}
Mind you, I havent’t coded in Javascript in a very very long time, and I haven’t tested out any of the actual code, but it should all work. In any case, the theory is there. I wish you the best of luck!