简体   繁体   English

如何使用C#在Unity 3D中的随机时间(相同位置)生成敌人?

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

I generate repeatedly enemies each 1.75f . 我每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. 我的原型游戏就像Chrome浏览器中的游戏,当找不到页面时就会显示。

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);
            }
        }   
    }
}

You can use StartCoroutine for simple enemy instantiating: 您可以使用StartCoroutine进行简单的敌人实例化:

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: 而且,正如Andrew Meservy所说,如果您想为计时器添加随机性(例如,使生成延迟从0.5秒随机变化到2.0秒),则只需将yield return替换为该值即可:

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

Modified version to generate the enemies 修改版本以产生敌人

Use StartCoroutine with Random Time 随机使用StartCoroutine

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; 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);
            }
        }   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM