unity3d c#衍生间隔变更程式

ndasle7k  于 2022-11-30  发布在  C#
关注(0)|答案(2)|浏览(184)

我在下面添加了我的代码。我的错误是什么,有人能帮助我吗?我想当SpawnRandomBall函数运行两次时,spawnInternal会变成spawnInternal2。所以我创建了一个新变量,调用“check”。SpawnRandomBall函数运行时变量增加。我将变量设置为公共变量。这样我就可以看到“check”变量增加或不增加。“Check”变量增加没有问题。当可验证值等于3时,必须运行“else if”。但不幸的是,它不起作用。
我想问题是我在Start()函数中运行我的代码。但是我不知道我该怎么做。

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

public class SpawnManagerX : MonoBehaviour
{
    public GameObject[] ballPrefabs;

    private float spawnLimitXLeft = 14.5f;
    private float spawnLimitXRight = 24;
    private float spawnPosY = 10;

    private float startDelay = 1.0f;
    private float spawnInterval = 4.0f;
    private float spawnInterval2 = 2.0f;
    public int check;

    // Start is called before the first frame update
    void Start()
    {
        if (check <= 2)
        {
            InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
        }
        else if (check > 2)
        {
            InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval2);
        }
    }
    // Spawn random ball at random x position at top of play area
    void SpawnRandomBall ()
    {
        // Generate random ball index and random spawn position
        Vector3 spawnPos = new Vector3(-21, spawnPosY, Random.Range(spawnLimitXLeft, spawnLimitXRight));
        int ballIndex = Random.Range(0, 3);

        // instantiate ball at random spawn location
        Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
        check += 1;
    }

}

我要将SpawnInternal变量更改为SpawnInternal2

f0brbegy

f0brbegy1#

你所描述的情况是,你想要一个方法运行。在前两次迭代中,你想要一个特定的延迟间隔,然后你想要一个不同的延迟间隔。在这种情况下,我建议你使用错误的工具。我个人会启动一个如下的协程:

public class SpawnManagerX : MonoBehaviour
{
    public GameObject[] ballPrefabs;

    private float spawnLimitXLeft = 14.5f;
    private float spawnLimitXRight = 24;
    private float spawnPosY = 10;

    private float startDelay = 1.0f;
    private float spawnInterval = 4.0f;
    private float spawnInterval2 = 2.0f;
    public int check;

    void Start()
    {
        StartCoroutine ( SpawnRandomBall () );
    }

    // Spawn random ball at random x position at top of play area
    IEnumerator SpawnRandomBall ()
    {
        while ( true )
        {
            // yielding here will produce an initial delay when the coroutine is run.
            yield return new WaitForSeconds ( 
            (check++ < 2) ? spawnInterval : spawnInterval2 );

            // Generate random ball index and random spawn position
            var spawnPos = new Vector3(-21, spawnPosY, Random.Range(spawnLimitXLeft, spawnLimitXRight));
            var ballIndex = Random.Range(0, 3);

            // instantiate ball at random spawn location
            Instantiate( ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation );
        }
    }
}

也可以将Start方法作为协程来运行,但是我觉得,特别是对于新手来说,有时候可能会丢失意图。
这就是说,启动一个协程,在那个协程中,不断循环while (true)(意思是“永远”).改变等待时间的那一行是最后一行,“return”.我们WaitForSeconds,等待的秒数是基于当前的计数值.注意:写在手机上,但没有测试。

  • 编辑以在协程中创建初始延迟。*
zpf6vheq

zpf6vheq2#

如果Start被调用一次,那么else if就永远不会运行。代码不会只是在else if等待check更新。
您只需要管理在SpawnRandomBall方法中运行的代码。
你得试试这个

// Start is called before the first frame update
void Start()
{
    InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval2);
}
// Spawn random ball at random x position at top of play area
void SpawnRandomBall()
{
    check++;
    if (check == 1 || check == 3)
        return;
        
    // Generate random ball index and random spawn position
    Vector3 spawnPos = new Vector3(-21, spawnPosY, Random.Range(spawnLimitXLeft, spawnLimitXRight));
    int ballIndex = Random.Range(0, 3);

    // instantiate ball at random spawn location
    Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
}

相关问题