C# spawn管理器快速创建一波又一波的敌人。

问题描述 投票:0回答:1

我是一个初学者,按照Unity的Create With Code课程,在Unity中制作我的第一个游戏。我正在创建一个射击游戏,将使用手部跟踪。我还没有设置手部追踪,所以我创建了一个OnTrigger输入,当我击中空间时,会爆炸物体。我创建了下面的产卵管理器来产卵一波波的敌人攻击,但是他们所有的波浪都产卵太快了。似乎它们是自动生成的,而不是在第一波被摧毁后才生成。

有没有一种更简单的方法来以较慢的速度产卵?或者只在没有更多敌人活着的时候才产卵?

EDIT: 添加了下面的攻击脚本

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

public class SpawnAttack : MonoBehaviour
{
    public GameObject Trumps;
    private float spawnRange = 9;
    public int enemyCount;
    public int waveNumber = 1;

    void Start()
    {      
        SpawnEnemyWave(waveNumber);
        //InvokeRepeating("GenerateSpawnPosition", startDelay, randomInterval); 
    }

    // Update is called once per frame
    void Update()
    {
        enemyCount = FindObjectsOfType<Attack>().Length;
         if(enemyCount == 0)
             {
                waveNumber++;
                SpawnEnemyWave(waveNumber);
             }
     }   

    void SpawnEnemyWave(int enemiesToSpawn)
    {
        for (int i = 0; i < enemiesToSpawn; i++)
        {
            Instantiate(Trumps, GenerateSpawnPosition(), Trumps.transform.rotation);
        }
    }

    private Vector3 GenerateSpawnPosition()
     {
        float spawnPosX = Random.Range(-spawnRange, spawnRange);
        float spawnPosZ = Random.Range(-spawnRange, spawnRange);
        Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
        return randomPos;      
     }
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour
{
    public float speed = 0.5f;
    public GameObject Player;
    private Rigidbody enemyRb;


    // Start is called before the first frame update
    void Start()
    {
        enemyRb = GetComponent<Rigidbody>();
        Player = GameObject.Find("Player");
        this.transform.LookAt(Player.transform);

    }

    // Update is called once per frame
    void Update()
    {

        Vector3 lookDirection = (Player.transform.position - transform.position).normalized;
        enemyRb.AddForce(lookDirection * speed);
        transform.Translate(Vector3.forward * Time.deltaTime * speed);


    }
    private void OnTriggerEnter(Collider other)
    {
        Destroy(gameObject);
        Debug.Log("Game Over");
    }  
}

c# unity3d
1个回答
0
投票

我想你可以使用 invoke 方法。

Invoke("NameOfTheMethod", 1f)

这个方法的作用是在调用方法前等待一定的时间。你必须把方法的名字写在引号里,然后选择你想在方法被调用之前等待多长时间("1f "代表延迟的时间,以秒为单位),在你的情况下,你可以让脚本在产生敌人之前等待。


0
投票

我不知道你的 Attack 脚本,但我会使用类似

public class Attack : MonoBehaviour
{
    public static readonly HashSet<Attack> AliveAttackInstances = new HashSet<Attack>();

    private void Awake()
    {
        AliveAttackInstances.Add(this);
    }

    private void OnDestroy()
    {
        AliveAttackInstances.Remove(this);
    }
}

所以,你可以一直在一个更便宜的方式检查有多少,哪些敌人是活着的,像。

public class SpawnAttack : MonoBehaviour
{
    // I would change this type here to make sure your spawned prefab actually 
    // HAS an Attack attached .. otherwise your enemyCount will always be 0
    public Attack Trumps;

    ...

    void Update()
    {
        if(Attack.AliveAttackInstances.Count == 0)
        {
            waveNumber++;
            SpawnEnemyWave(waveNumber);
        }
    }   

然后,为了在产生下一波之前增加一定的延迟,你可以使用一个简单的计时器,如

public class SpawnAttack : MonoBehaviour
{
    public Attack Trumps;

    [SerializeField] private float delay = 1f;
    private float timer;

    ...

    void Update()
    {
        if(Attack.AliveAttackInstances.Count == 0)
        {
            timer -= Time.deltaTime;
            if(timer <= 0)
            {
                timer = delay;

                waveNumber++;
                SpawnEnemyWave(waveNumber);
            }
        }
    }   

0
投票

尝试使用Coroutine.这里有一个关于Coroutines的视频。https:/www.youtube.comwatch?v=qolMYyq0nX0

我的例子

public class Spawn : MonoBehaviour {

private float TimeToWait = 3f;

public int enemyCount = 0;

public int waveNumber = 0;

public GameObject enemy;

void Start()
{
    StartCoroutine(SpawnEnemyWave(waveNumber));
}

void Update()
{
    if (enemyCount == 0)
    {
        waveNumber++;
        StartCoroutine(SpawnEnemyWave(waveNumber));
    }

    if (Input.GetMouseButtonDown(0))
    {
        enemyCount--;
    }
}

IEnumerator SpawnEnemyWave(int enemiesToSpawn)
{
    //"Things to do before the seconds."
    for (int i = 0; i < enemiesToSpawn; i++)
    {
        enemyCount++; 
    }

    yield return new WaitForSeconds(TimeToWait); // at this example we wait 3 seconds (float TimeToWait = 3f;)

    //"Things to do after the seconds."

    for (int i = 0; i < enemiesToSpawn; i++)
    {
        Debug.Log("New Enemy!");

        Instantiate(enemy, transform.position, Quaternion.identity);
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.