- Home /
Monster Spawner
Hence the title, I just want a monster spawner that spawns a monster every xxx frames. I pretty confident as making the "every xxx frames" part of it, but I have 7 vector3 variables and i want a monster to spawn at random at any one of those locations. I think I can make the code to choose the random of the 7 vector3 variables.
What I don't know how to do is IN CODE, 'spawn' or place a prefab in space USING CODE. (I'd prefer javascript, don't know a bit of C#.)
Then I think I know how to move the monster to the given location.
Anyone know how to do this, or at least point me in the right direction? Any help would be much appreciated. Thanks! :)
Answer by Imankit · Jan 17, 2013 at 11:26 AM
Use something like this..:
var randPos = 0;
// In your function or Update
randPos = Random.Range(0,7);
// When you want to position the monster out of those 7 positions say pos1,pos2,pos3,pos4,pos5,pos6,pos7, just call this function
pos(randPos);
// pos function
function pos(a)
switch(a){
case 0 : monster.transform.position = pos1;
break;
case 1 : monster.transform.position = pos2;
break;
case 2 : monster.transform.position = pos3;
break;
case 3 : monster.transform.position = pos4;
break;
case 4 : monster.transform.position = pos5;
break;
case 5 : monster.transform.position = pos6;
break;
case 6 : monster.transform.position = pos7;
break;
}
Answer by cdrandin · Jan 23, 2013 at 06:19 AM
You could do something blunt like.
public rate : int = 1;
private lastTime : float;
...//Down the line where you have your monster.transform.position
for(var n = 0; n < rate; n++)
{
Instantiate(monster, monster.transform.position, monster.transform.rotation);
lastTime = Time.time;
}
//Simply create one per frame which imo is kind of fast, but is a very simple idea. Might want to play with the yield WaitForSeconds to add some delay.
Then after Time has passed, let's say the variable rateTimer, is how many seconds till our rate will increase to spawn more at a time.
soo...
if((Time.time - lastTime) > rateTimer)
{
rate +=1; //so our rate increase after time has passed and will keep doing this up to infinity.
}
The code isn't tested and is more a reflection of the idea.
Also, it is possible for the monster to spawn at the same position and if they have colliders are more likely to cause some problems unless you ignore collision with the monsters, which you can have a tag "monster"
Thanks heaps! Only one more thing, is it possible to use 'Instantiate' on another object, for example I create a "monster spawn" object and clone the monster object?
EDIT: Forget all of that. That's answered my quesition. Thanks!
It is never wise to Instantiate a copy of an object on the scene. It is why Unity has prefabs for organization. It is possible to answer your question, but I wouldn't do it.
Answer by DayyanSisson · Jan 23, 2013 at 06:41 AM
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!
Your answer
Follow this Question
Related Questions
Destroy and Spawn an Enemy 1 Answer
Enemy spawn script/player position(java/js) 1 Answer
How to spawn Prefab to a certain position. 2 Answers
choose 1 of multiple objects to spawn, respawn once its dead. 3 Answers
Spawn object in random areas 2 Answers