微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使难度按钮选项选择更改生成率

如何解决如何使难度按钮选项选择更改生成率

我正在尝试让我的 Unity 手机游戏难度选项按钮选择(简单/中等/困难)功能改变敌人 ai 的生成率。例如,在所有关卡/场景中,点击“中等”应该会触发 2 倍的人工智能生成

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

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

难度按钮脚本:

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,Quaternion.identity);
                    break;
                case 3:
                    Instantiate(prefab3,Quaternion.identity);
                    break;
                case 4:
                    Instantiate(prefab4,Quaternion.identity);
                    break;
               
            }
         
            nextSpawn = Time.time + spawnRate;
        }
    }
}

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

enter image description here

解决方法

我认为您的问题是按钮链接到您的 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);});  
}
}

随机生成类

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,Quaternion.identity);
                break;
            case 3:
                Instantiate(prefab3,Quaternion.identity);
                break;
            case 4:
                Instantiate(prefab4,Quaternion.identity);
                break;
           
        }
     
        nextSpawn = Time.time + spawnRate;
    }
}

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。