fedevmoreno
fedevmoreno

Reputation: 29

How can I generate enemies in random time (same position) in Unity 3D using C#?

I generate repeatedly enemies each 1.75f. But I don't know how can I use a Random function. My prototype game is like the game in Chrome Browser,It appears when the page is not found.

Thank you for helping me.

Here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyGeneratorController : MonoBehaviour
    {
        public GameObject enemyPrefeb;
        public float generatorTimer = 1.75f;

        void Start () 
        {

    }

    void Update ()
    {

    }

    void CreateEnemy()
    {
        Instantiate (enemyPrefeb, transform.position, Quaternion.identity);
    }

    public void StartGenerator()
    {
        InvokeRepeating ("CreateEnemy", 0f, generatorTimer);
    }

    public void CancelGenerator(bool clean = false)
    {
        CancelInvoke ("CreateEnemy");
        if (clean)
        {
            Object[] allEnemies = GameObject.FindGameObjectsWithTag ("Enemy");
            foreach (GameObject enemy in allEnemies)
            {
                Destroy(enemy);
            }
        }   
    }
}

Upvotes: 2

Views: 2260

Answers (2)

Jasil
Jasil

Reputation: 175

Modified version to generate the enemies

Use StartCoroutine with Random Time

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyGeneratorController : MonoBehaviour
    {
        public GameObject enemyPrefeb;
         public float generatorTimer { set; get; }

        void Start () 
        {

generatorTimer = 1.75f;

    }

    void Update ()
    {

    }

    void IEnumerator CreateEnemy()
    {
        Instantiate (enemyPrefeb, transform.position, Quaternion.identity);
        yield return new WaitForSeconds(generatorTimer);
        generatorTimer = Random.Range(1f, 5f);
    }

    public void StartGenerator()
    {
        StartCoroutine(CreateEnemy());

    }

    public void CancelGenerator(bool clean = false)
    {
        CancelInvoke ("CreateEnemy");
        if (clean)
        {
            Object[] allEnemies = GameObject.FindGameObjectsWithTag ("Enemy");
            foreach (GameObject enemy in allEnemies)
            {
                Destroy(enemy);
            }
        }   
    }
}

Upvotes: 1

caxapexac
caxapexac

Reputation: 914

You can use StartCoroutine for simple enemy instantiating:

using System.Collections;

...

private IEnumerator EnemyGenerator()
{
    while (true)
    {
        Vector3 randPosition = transform.position + (Vector3.up * Random.value); //Example of randomizing
        Instantiate (enemyPrefeb, randPosition, Quaternion.identity);
        yield return new WaitForSeconds(generatorTimer);
    }
}

public void StartGenerator()
{
    StartCoroutine(EnemyGenerator());
}

public void StopGenerator()
{
    StopAllCoroutines();
}

And, as Andrew Meservy said, if you wanna add the randomness to the timer (for example to make spawn delay random from 0.5 sec to 2.0 sec) then you can just replace yield return to this one:

yield return new WaitForSeconds(Mathf.Lerp(0.5f, 2.0f, Random.value));

Upvotes: 6

Related Questions