Question by
muflichkamil · Feb 21, 2016 at 01:57 AM ·
cameratagspawningspawning problemsspawner
Check if there's no GameObject with specific tag in the field (in the camera view)
Hi there, I have a simple 2D game, and right now I have the enemy spawning flow based on time interval. But it has weakness, that when the enemy in field has been destroyed quickly, user need to wait several seconds left until next enemy appear, and its not challenging.
Whaat I am trying to achieve, is still the time interval-based, but when the enemy in field has been destroyed, the spawner script will bypass its interval and spawn an enemy instead. Is it possible? Anyone have used this technique? Have been browsing for a while and still no luck.
Current enemy spawner,
public GameObject[] enemyCharacterSpawn;
// Update is called once per frame
void Update () {
foreach(GameObject thisAttacker in enemyCharacterSpawn){
if(isTimeToSpawn(thisAttacker))
Spawn(thisAttacker);
}
}
void Spawn (GameObject myGameObject){
GameObject myAttacker = Instantiate (myGameObject) as GameObject;
myAttacker.transform.parent = transform;
myAttacker.transform.position = transform.position;
}
bool isTimeToSpawn (GameObject attackerGameObject){
SpawnerNew attacker = attackerGameObject.GetComponent<SpawnerNew>();
float meanSpawnDelay = attacker.appearEveryXSeconds;
float spawnPerSecond = 1/meanSpawnDelay;
if(Time.deltaTime > meanSpawnDelay) {
Debug.LogWarning("WoooopS");
}
float threshold = spawnPerSecond * Time.deltaTime / 4;
if(Random.value < threshold) {
return true;
} else {
return false;
}
}
Comment