我是C#语言的新手,我试图做一个简单的游戏。对象从0速度开始。我想逐渐提高对象的速度,直到达到1200,直到达到1200的那一刻停止增加并立即逐渐降低它的速度,直到达到0。但是该功能在update方法中,所以我被卡在了这里。谁能帮我这个忙。非常感谢。
void Update()
{
// I got stuck here
if(speed >= 0 && speed < 1200)
{
speed++;
}
Debug.Log(speed);
// this is where the speed was put in and the object start to move.
if (spin)
{
foreach (Transform image in transform)
{
{
image.transform.Translate(Vector3.down * Time.smoothDeltaTime * speed, Space.World);
if (image.transform.position.y <= 0)
{
image.transform.position = new Vector3(image.transform.position.x, image.transform.position.y + 420, image.transform.position.z);
}
}
}
}
您可以添加一个布尔变量来帮助您:
private bool decrease = false;
void Update()
{
if(speed >= 0)
{
if (speed == 1200)decrease = true;
if (decrease)
if (speed > 0) speed--;//dunno if you want to block at 0 when decreasing
else
speed++;
}
Debug.Log(speed);