【问题标题】:Unity: How to speed up score countdownUnity:如何加快分数倒计时
【发布时间】:2018-01-06 14:59:01
【问题描述】:

一个简单的问题。我正在玩一些教程中的一个小游戏,但想添加一个每秒减少的分数。我通过下面提供的代码设法做到了这一点,但分数只降低了 1 秒。我想以某种方式加速它,所以它可以每秒下降 150。我尝试了一些东西,但它们没有工作,其中大多数甚至没有记录游戏内 GUI 的变化。任何帮助表示赞赏!

代码:

public class GameOver : MonoBehaviour {

    public GameObject gameOverScreen;
    public Text Score;
    public Text Highscore;

    bool gameOver;
    private int score = 12000;

    void Start () {
        FindObjectOfType<PlayerController>().OnPlayerDeath += OnGameOver;
    }


    public void Update () {

        Score.text = Mathf.Round(score - Time.timeSinceLevelLoad).ToString();

        if (gameOver)
        {
            if (Input.GetKeyDown (KeyCode.Space))
            {
                SceneManager.LoadScene(1);
            }
        }
    }

     void OnGameOver()
    {
        gameOverScreen.SetActive (true);
        Highscore.text = Mathf.Round(score - Time.timeSinceLevelLoad ).ToString();

        gameOver = true;
    }
}

【问题讨论】:

    标签: c# unity3d unity5 unity3d-2dtools


    【解决方案1】:

    不要重复自己。制作一个函数来获取分数。

    int GetScore() {
        return score - (int)Time.timeSinceLevelLoad * 150;
    }
    

    然后使用它。

    void Update() {
        Score.text = GetScore().ToString();
    }
    

    【讨论】:

    • 谢谢!完全按照我的意愿工作。我不知道你可以用 (int) 做到这一点。
    【解决方案2】:

    改变这部分

     Score.text = Mathf.Round(score- Time.deltaTime*150).ToString(); 
    

    --

    public void Update () {
    
                Score.text = Mathf.Round(score- Time.deltaTime*150).ToString();
    
                if (gameOver)
                {
                    if (Input.GetKeyDown (KeyCode.Space))
                    {
                        SceneManager.LoadScene(1);
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多