按时间分数乘数

时间:2016-04-22 18:04:46

标签: unity3d time multiplication unity5 seconds

这是我制作的得分管理器脚本:

using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;

 public class ScoreManager : MonoBehaviour {

     public Text scoreText;

     public float scoreCount; // What the score is when the scene first loads. I set this as zero.

     public float pointsPerSecond; // Every second, the points increase by THIS amount. Right now it is 100.

     // Update is called once per frame
     void Update () {

         scoreCount += pointsPerSecond * Time.deltaTime;

         scoreText.text = "" + Mathf.Round (scoreCount); // It comes out as a float with like 6 decimals; I round to an nice, clean Integer 

     }
 }

我无法弄清楚如何解决的问题是:如何在游戏30秒后制作一个乘以得分2倍的乘数,然后将得分乘以1分钟后的3倍,然后乘以4倍1分30秒,最后是2分钟后的5倍?谢谢:))

2 个答案:

答案 0 :(得分:4)

private float multiplier = 2.0f;
void Start(){
    InvokeRepeating("SetScore", 30f, 30f);
}
void SetScore(){
    score *= multiplier;
    multiplier += 1f;
    if(multiplier > 5.5f) // Added 0.5f margin to avoid issue of float inaccuracy
    {  
        CancelInvoke()
    } 
}

InvokeRepeating设置第一个调用(第二个参数)和频率(第三个参数),在你的情况下它是30秒和30秒。然后,一旦乘数太大(大于5),就取消调用。

如果您的乘数是整数,则可以移除边距并使用整数。

答案 1 :(得分:0)

这是使用IEnmurator功能的绝佳机会。这些是您可以调用的方法,您可以告诉它们在恢复之前等待一段时间。因此,在您的情况下,您可以使用IEnumerator函数将您的分数乘以每30秒。例如:

public Text scoreText;

public float scoreCount; // What the score is when the scene first loads. I set this as zero.

public float pointsPerSecond; // Every second, the points increase by THIS amount. Right now it is 100.

private int scoreMultiplier = 1;

void Start()
{
    StartCoroutine(MultiplyScore());
}

// Update is called once per frame
 void Update () 
{

     scoreCount += pointsPerSecond * Time.deltaTime;

     scoreText.text = "" + Mathf.Round (scoreCount); // It comes out as a float with like 6 decimals; I round to an nice, clean Integer 

 }

IEnumerator MultiplyScore()
{
    while(true)
    {
        yield return new WaitForSeconds(30);
        scoreMultiplier++;
        scoreCount *= scoreMultiplier;
    }
}

注意,如果您只想进行5倍乘法,则可以使用得分乘数变量作为IEnumerator while循环中的条件,如下所示:

IEnumerator MultiplyScore()
{
    while(scoreMultiplier < 5)
    {
        yield return new WaitForSeconds(30);
        scoreMultiplier++;
        scoreCount *= scoreMultiplier;
    }
}