Try placing `spawned = 1;` before the call to the function spawnend.
It also looks as though you are resetting spawned to 0 at the end of spawnend and have the call for spawnend in the update function. The update function is called every frame so upwards to 60 times in a second. This can have adverse affects.
You should check the time between the last call to spawnend.
declare a variable to store the time value.
var timeLastSpawnEnd = Time.time;
Then change the following code.
if(player){
var pdis1 = Vector3.Distance(player.transform.position, end.position);
var pdis2 = Vector3.Distance(player.transform.position, start.position);
if(pdis1 <= 4000){
if(spawned == 0 && Time.time > timeLastSpawnEnd +1 ){
spawnend();
spawned = 1;
}
}
}
}
function spawnend(){
//yield WaitForSeconds(5.0);
var terrainss = Instantiate(terrain[ttype], Vector3(transform.position.x + terrain[ttype].GetChild(0).collider.bounds.size.x, transform.position.y,7), Quaternion.Euler(0,0,0));
timeLastSpawnEnd = Time.time;
var ts : randomhillspawn = terrainss.GetComponent("randomhillspawn");
ts.spawned = 0;
}
This should allow you to make a call to spawnend only once every second.
↧