首页 > 解决方案 > 我如何添加一行来告诉代码我希望平台在关卡循环完成后加速/

问题描述

public class PlatformMoving : MonoBehaviour
{
    public float speed = 1.5f; //How fast the platforms are moving

    // use this for initialization
    void Start()
    {

    }

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

        // Every frame we look at the position of the ground and it is moved to the left
        transform.position = transform.position - (Vector3.right * speed * Time.deltaTime);

        // If the position of the ground is off the left of the screen
        if (transform.position.x <= -13.05f)
        {
            // Move it to the far right of the screen
            transform.position = transform.position + (Vector3.right * 53.3f);
        }
    }
}

标签: c#unity3d

解决方案


假设您正在谈论的循环是注释后面的行// If the position of the ground is off the left of the screen。速度的增加将在其后的if语句中指定,因为这是循环发生的地方。

我会避免称它为loopbtw,只是因为它在浏览代码时会搜索 a或循环for,当不存在时会导致混乱。whileforeach

我已经在您的代码中评论了它所在的位置。

public class PlatformMoving : MonoBehaviour
{
    public float speed = 1.5f; //How fast the platforms are moving

    // use this for initialization
    void Start()
    {

    }

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

        // Every frame we look at the position of the ground and it is moved to the left
        transform.position = transform.position - (Vector3.right * speed * Time.deltaTime);

        // If the position of the ground is off the left of the screen
        if (transform.position.x <= -13.05f)
        {
            // Move it to the far right of the screen
            transform.position = transform.position + (Vector3.right * 53.3f);

            // Increase speed here
            // speed += x;
        }
    }
}

推荐阅读