simple enemy spawning Unity code example
Example: simple enemy spawning Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManagerScript : MonoBehaviour
{
//This variables will be used to make the limits of the spawn area
public Vector2 EnemyAreaLimitUpLeft;
public Vector2 EnemyAreaLimitDownRight;
//
public float TimeBetweenEnemiesSpawn;
public GameObject EnemyPrefab;
//This bool will be used to check when to spawn the enemy
private bool CanSpawn = true;
private void Update()
{
if (CanSpawn)
StartCoroutine(SpawnEnemies());
}
private IEnumerator SpawnEnemies()
{
CanSpawn = false;
GameObject EnemyInstantieted = Instantiate(EnemyPrefab);
EnemyInstantieted.transform.position = new Vector2(Random.Range(EnemyAreaLimitUpLeft.x, EnemyAreaLimitDownRight.x), Random.Range(EnemyAreaLimitUpLeft.y, EnemyAreaLimitDownRight.y));
yield return new WaitForSeconds(TimeBetweenEnemiesSpawn);
CanSpawn = true;
}
}