Answers for "simple enemy spawning Unity"

C#
0

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;

    }

}
Posted by: Guest on April-15-2021

Code answers related to "simple enemy spawning Unity"

C# Answers by Framework

Browse Popular Code Answers by Language