Unity 游戏中的生成率不会改变

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

在我的 Unity 游戏中,有门生成并朝向玩家。在每个门之后,游戏会变得更快,因此应根据游戏速度降低生成率。我试图降低每个产卵的产卵率,但它不起作用。

public void Start()
{
    InvokeRepeating("SpawnPrefab", .5f, spawnRate);
}

public void SpawnPrefab()
{
    int count = 0;

    int index = Random.Range(0, 5);
    int index2 = Random.Range(0, 5);
    int index3 = Random.Range(0, 5);
    int index4 = Random.Range(0, 5);
    int index5 = Random.Range(0, 5);

    int[] indexler = { index, index2, index3, index4, index5 };

    for (int i = 0; i < indexler.Length; i++)
    {
        
        if(indexler[i] == 2 || indexler[i] == 3 || indexler[i] == 4){ 
            count++;
        }

    }
    if(count == 5)
    {
        indexler[Random.Range(0, 5)] = Random.Range(0,2);
        Debug.Log("Count 5 OLDU");
    }

    Vector3 position5 = new Vector3(25f, 5f, zİlk);

    Instantiate(prefabs[indexler[0]],position5 , prefabs[0].transform.rotation);
    
    
    Vector3 position4 = new Vector3(15f, 5f, zİlk);

    Instantiate(prefabs[indexler[1]], position4, prefabs[0].transform.rotation);
    

    Vector3 position3 = new Vector3(5f, 5f, zİlk);

    Instantiate(prefabs[indexler[2]], position3, prefabs[0].transform.rotation);
    

    Vector3 position2 = new Vector3(-5f, 5f, zİlk);

    Instantiate(prefabs[indexler[3]], position2, prefabs[0].transform.rotation);
    

    Vector3 position1 = new Vector3(-15f, 5f, zİlk);

    Instantiate(prefabs[indexler[4]], position1, prefabs[0].transform.rotation);


    spawnRate -= spawnRate * 15 / 100;

}

我使用 Debug.Log 检查了 spawnrate 值:它发生了变化,但它不能在 Invokerepeating 中采用新值。我认为问题的原因是因为在 Start 函数中调用了 ınvokerepeating 函数,所以它的值永远不会改变。但我找不到任何解决方案。是否可以在更新函数中调用 ınvokerepeating ?

c# unity3d game-development spawn
1个回答
0
投票

这里的主要问题是你在使用

InvokeRepeating("SpawnPrefab", .5f, spawnRate);

这是阅读once你的价值

spawnRate
你打电话的那一刻。以后对
spawnRate
的更改无关紧要!

=>为了保持这种动态,根本不要使用

InvokeRepeating
,而是使用一个简单的计时器,例如

const float DECREASE_FACTOR = 15f / 100f;

// Starts negative for initial delay
float timePassed = -0.5f;

private void Update ()
{
    // track the passed time every frame
    timePassed += Time.deltaTime;

    // if it exceeds the spawn delay spawn
    // this also works now if the spawnRate is decreased/increased at any point
    if(timePassed >= spawnRate)
    {
        timePassed -= spawnRate;
        SpawnPrefab();
    }
} 

private void SpawnPrefab()
{
    ...   

    spawnRate -= spawnRate * DECREASE_FACTOR;
}
© www.soinside.com 2019 - 2024. All rights reserved.