GameObject的滚动速度不必要地增加

时间:2017-08-13 15:56:02

标签: c# unity5

在Unity中,我遇到了一个小问题。我以一定的速度开始产生一些产生(比方说2),并且随着时间的推移,速度增加(假设为0.5)。当玩家输了并加载游戏结束时,速度会在后台继续增加,所以当玩家再次尝试时,速度不是2(初始/正常)但它是3或4或(取决于多少时间已经过去了)。

我该如何解决这个问题?加载Gameover屏幕时,我应该销毁还是禁用脚本的执行?

代码:

public class ObjectsScroller : MonoBehaviour {

public float speed = 2; // Speed of the rings
int MaxSpeed = 5; // Max speed of the rings

int timeSecond = 1; // 1s
float timer;

int sixSeconds = 6; // 6 seconds without subtraction
int countdown = 6; // Countdown with subtraction
// Use this for initialization
void Start () 
{
    speed = 2;
    timeSecond = 1;
    timer = Time.time;
    countdown = 6;
}

void Update()
{
    timer = Time.time;

    transform.Translate(Vector2.up * speed * Time.deltaTime);

    if(timer >= timeSecond) { // Checks if a second passed
        countdown -= 1; // countdown decreases by one
        timeSecond += 1; // Increases so the loop runs again
    }

    if (countdown <= 0) { // Checks if countdown is less than 0
        speed += .5f; // Speed increases by 1
        transform.Translate(Vector2.up * speed * Time.deltaTime);
        countdown = sixSeconds; // Countdown is equal to 8 Seconds so the loop runs again
    }

    if (speed >= 5) { // If speed is bigger or equal to 5 then make it 5 only
        speed = MaxSpeed;
    }
}
}

0 个答案:

没有答案