OK, lets look at this in sections.
First, we need a way to figure out where the monster should come from. For this, I suggest a 3 part check. 1) a random direction, 2) a random distance and 3) a Linecast to determine if we can be put there.
To solve this, you would need a function that Gets a Random Position based on these factors.
function GetRandomPosition(){
var dir = Vector3.Scale(Random.insideUnitSphere, Vector3(1,0,1)).normalized;
var pos = transform.TransformPoint(dir * Random.Range(minRange, maxRange);
var hit : RaycastHit;
if(Physics.Linecast(transform.position, pos, hit)){
pos = hit.point + Vector3.Scale(hit.normal, Vector3(1,0,1)).normalized;
}
return pos;
}
Notice that the Vector3.Scales, this keeps everything level.
Next, we need to keep track of our monsters. To do this, I suggest a List object like so:
import System.Collections.Generic;
private var monsters : List.<GameObject>;
This might seem like overkill for simply adding a monster to the scene, but it will make sense later.
Next we need to have a function that will Add a monster to it, as well as set a facing for the monster initially.
var monsterPrefab : GameObject;
function AddMonster(){
monsters.Add(Instantiate(mosnterPrefab, GetRandomPosition(), Quaternion.identity);
LookAtPlayermonsters[monsters.Count - 1]);
}
function LookAtPlayer(go : GameObject){
go.transform.LookAt(transform);
go.localEulerAngles.x = 0;
}
With this, we can now track monsters. By using List.Count we can see how many monsters we currently have in our scene. So we will need some more tracking. This time, Time. Since you want them to come out 30 to 40 seconds after the first one dies. Also, we need to know when the monster dies. Lastly, we will keep track of the time that the monster should come out.
var minRange : float;
var maxRange : float;
private nextMonster : float = Mathf.infinity;
function Start(){
AddMonster();
}
function Update(){
var count = monsters.Count;
for(var i=0; i<monsters.Count; i++){
if(monsters[i] == null){
monsters.RemoveAt(i);
i--;
}
}
if(monsters.Count > count nextMonster == Mathf.infinity){
nextMonster = Time.time + 30 + Random.Range(0, 10);
}
if(Time.time > nextMonster){
AddMonster();
nextMonster = Mathf.infinity;
}
}
OK, so lets walk through it.
We start by adding a monster. this is done in a random position according to GetRandomPosition. The monster is set to look at the player (note that we also set the x rotation back to zero so he is upright)
Every frame, we check to see if the “Count” changes, if not we wait until next frame.
OK, we kill our monster, and the Update check sees that the model is no longer there. So it says. “Hey, our count is now less than what it was.” We set the nextMonster to the time that our next monster should appear (30 + 0 to 10)
Every frame, we then check to see if our time is up for introducing the next monster, if so, we add it and set nextMonster to infinity so it wont pop out another next frame.
OK, for some changes… Lets add this stuff:
// variables
var newMonsterEvery : float = 20;
private var newMonster : float = newMonsterEvery;
// add this to the Update
if(Time.time > newMonster){
AddMonster();
newMonster += newMonsterEvery;
}
Thsi code then simply adds a new mosnter every 20 seconds. And with the overkill List we are using, this now keeps track of all the monsters. Any time Any monster is killed it will trigger adding another, but not reset the nextMonster counter.