如何使难度按钮选项选择改变产卵率

How to make difficulty button option selection change spawn rate

我正在尝试让我的 Unity 手机游戏难度选项按钮选择 (Easy/Medium/Hard) 功能改变敌人 ai 的生成率。例如,单击“中”应该会在所有 levels/scenes.

中触发 2 倍的 ai 生成

我的按钮根据控制台工作,但难度按钮脚本无法与 Spawn 脚本正确通信,因为选择难度级别不会影响生成率。

有人看到我遗漏了什么吗?花了很多时间在这上面,但没有运气。

难度按钮脚本:

public class DifficultyButton : MonoBehaviour
{
    private Button button;

    private RandomSpawn randomSpawn;
    
    public int difficulty;

    
    
    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>(); 
        button.onClick.AddListener(SetDifficulty); 
        randomSpawn = GameObject.Find("Random Spawn").GetComponent<RandomSpawn>(); 
    }

    //click on diff buttons n Console will show they were clicked
    void SetDifficulty() 
    {
        Debug.Log(gameObject.name + " was clicked");
        
    }
}

生成脚本:

public class RandomSpawn : MonoBehaviour
{
   
    public GameObject prefab1, prefab2, prefab3, prefab4;
   
    public float spawnRate = 2f;
   
    private float nextSpawn = 0f;
   
    private int whatToSpawn;
   
 
    public bool isGameActive;
    public void StartGame(int difficulty)
    {
        isGameActive = true;
        spawnRate /= difficulty;
    }
 
    void Update()
    {
        if (Time.time > nextSpawn) { //if time has come
            whatToSpawn = Random.Range(1, 6); // define random value between 1 and 4 (5 is exclusive)
            Debug.Log(whatToSpawn); //display its value in console
           
           
            switch (whatToSpawn) {
                case 1:
                    Instantiate(prefab1, transform.position, Quaternion.identity);
                    break;
                case 2:
                    Instantiate(prefab2, transform.position, Quaternion.identity);
                    break;
                case 3:
                    Instantiate(prefab3, transform.position, Quaternion.identity);
                    break;
                case 4:
                    Instantiate(prefab4, transform.position, Quaternion.identity);
                    break;
               
            }
         
            nextSpawn = Time.time + spawnRate;
        }
    }
}

这是我在检查器中设置困难按钮的图片

我假设您的问题是按钮链接到您的 onClick,但是 onClick 没有做任何事情。据我所知,您正在设置的 onClick 仅具有 Debug.Log,因此我会尽力帮助解决该问题。

public class DifficultyButton : MonoBehaviour
{
private Button button;

private RandomSpawn randomSpawn;

public int difficulty;



// Start is called before the first frame update
void Start()
{
    button = GetComponent<Button>(); 
    randomSpawn = GameObject.Find("Random Spawn").GetComponent<RandomSpawn>();
    button.onClick.AddListener(delegate{randomSpawn.UpdateSpawnRate(difficulty);});  
}
}

随机生成Class

public class RandomSpawn : MonoBehaviour
{

public GameObject prefab1, prefab2, prefab3, prefab4;

public float maxSpawnRate = 2f;

private float nextSpawn = 0f;

private int whatToSpawn;
private float spawnRate = 2f;


public bool isGameActive;
public void StartGame(int difficulty)
{
    isGameActive = true;
    UpdateSpawnRate(difficulty);
}

void Update()
{
    if (Time.time > nextSpawn) { //if time has come
        whatToSpawn = Random.Range(1, 6); // define random value between 1 and 4 (5 is exclusive)
        Debug.Log(whatToSpawn); //display its value in console
       
       
        switch (whatToSpawn) {
            case 1:
                Instantiate(prefab1, transform.position, Quaternion.identity);
                break;
            case 2:
                Instantiate(prefab2, transform.position, Quaternion.identity);
                break;
            case 3:
                Instantiate(prefab3, transform.position, Quaternion.identity);
                break;
            case 4:
                Instantiate(prefab4, transform.position, Quaternion.identity);
                break;
           
        }
     
        nextSpawn = Time.time + spawnRate;
    }
}

public void UpdateSpawnRate(int difficulty)
{
    spawnRate = maxSpawnRate / difficulty;
}
}

我不确定你的按钮是否出现在游戏之前,然后你 select 难度然后单击另一个调用 StartGame() 的按钮。无论哪种方式,我现在正在做的是直接将难度按钮的 onClick 按钮设置为委托函数 UpdateSpawnRate(),这将更新您当前的 spawnRate。我还将 spawnRate 的 public 变量更改为 maxSpawnRate 以防您多次更改难度或玩多轮。拥有最大值允许现在的临时 spawnRate 根据 maxSpawnRatedifficulty 变量相对变化。

让我知道此代码是否有效,我将解决方案类型作为要遵循的一般方向,而不是确切的答案。如果您需要有关如何实施它的更多指导或代码中的某些内容不起作用,请在评论中告诉我。