按时间得分乘数
Score Multiplier By Time
这是我制作的乐谱管理器脚本:
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 秒后将分数乘以二,然后在 1 分钟后将分数乘以三,然后乘以1 分 30 秒后四次,最后,2 分钟后乘以五次?谢谢:)
这是使用 IEnmurator
功能的绝好机会。这些是您可以调用的方法,您可以告诉它们在恢复之前等待一段时间。所以在你的情况下,你可以有一个 IEnumerator
函数,每三十秒乘以你的分数。例如:
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;
}
}
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设置第一次调用(第二个参数)和频率(第三个参数),在你的情况下是30s,也是30s。然后一旦乘数太大(大于 5),你就取消调用。
如果您的乘数是整数,您可以去掉边距并使用整数。
这是我制作的乐谱管理器脚本:
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 秒后将分数乘以二,然后在 1 分钟后将分数乘以三,然后乘以1 分 30 秒后四次,最后,2 分钟后乘以五次?谢谢:)
这是使用 IEnmurator
功能的绝好机会。这些是您可以调用的方法,您可以告诉它们在恢复之前等待一段时间。所以在你的情况下,你可以有一个 IEnumerator
函数,每三十秒乘以你的分数。例如:
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;
}
}
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设置第一次调用(第二个参数)和频率(第三个参数),在你的情况下是30s,也是30s。然后一旦乘数太大(大于 5),你就取消调用。
如果您的乘数是整数,您可以去掉边距并使用整数。