Z
Z
Zimaell2020-06-20 12:46:49
Game development
Zimaell, 2020-06-20 12:46:49

How to set the sequence and time of appearance of enemies?

I have 3 prefabs (enemies), each one has an "Enemy" script attached with their behavior, the number of lives and their speed are configured in the prefabs themselves.

spoiler
public class Enemy : MonoBehaviour{
  public float MaxLife;
  public float speed;
  ..........
  }

There is a "Spawner" object where they are created and from there they keep the path, the script of the same name is attached to it
spoiler
public class Spawner : MonoBehaviour{
  public GameObject enemy1;
  public GameObject enemy2;
  public GameObject enemy3;
  
  public float spawnTime = 3f;
  public float startSpawnTime = 0f;
  private float timer =0;

  void Update(){
    timer -= Time.deltaTime;
    startSpawnTime += Time.deltaTime;
    if(timer <= 0 ){
      if(startSpawnTime > 0f && startSpawnTime < 9f) Instantiate(enemy1, transform.position, transform.rotation);
        else if(startSpawnTime > 9f && startSpawnTime < 18f) Instantiate(enemy2, transform.position, transform.rotation);
          else if(startSpawnTime > 18f && startSpawnTime < 30f) Instantiate(enemy3, transform.position, transform.rotation);
      timer = spawnTime;
      }
    }
  }

In this case, they appear from conditions over time.

How to set a sequence array when and which enemy should appear?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-06-20
@freeExec

class SpawnCondition
{
    float TimeFromStart;
    bool HasSpawned;
    GameObject Prefab;
}

foreach(cond in spawnConditions)
{
    if (!cond.HasSpawned && cond.TimeFromStart < currentTimeFromStart)
    {
        Instantiate(cond.Prefab);
        cond.HasSpawned = true
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question