2017-01-13 8 views
1

私は団結で初心者だと私は、ゲームが開始されてからスコアを5秒ごとに10ポイントを追加できるようにしたいこんにちは、これは私がそれを時間ベースのスコアリングユニティ

private int score; 
void Update() { 

    Timer = Time.time; 

    if (Timer > 5f) { 

     score += 5; 
     Timer -= 5f; 
    } 
    ScoreText.text = score.ToString(); 

} 
を実装しようとした方法です

これはうまくいかず、5f後にスコアが急上昇してゲームがクラッシュすることがあります。

答えて

2

5秒ごとに計算する計算が間違っています。 Timer = Time.time;すべてのループを実行するべきではなく、それはちょうどTimerの古い値を放棄します。 Time.deltaTimeを使用して、代わりにタイマーに追加してください。

//Be sure to assign this a value in the designer. 
public Text ScoreText; 

private int timer; 
private int score; 

void Update() { 

    timer += Time.deltaTime; 

    if (timer > 5f) { 

     score += 5; 

     //We only need to update the text if the score changed. 
     ScoreText.text = score.ToString(); 

     //Reset the timer to 0. 
     timer = 0; 
    } 
} 
+0

ありがとうございます! – user3679986

+0

あなたがそれを受け入れるときは、必ず答えてください。 –

関連する問題