加速和減速

暖暖De幸福發表於2024-07-20

private float rotateSpeed = 0;

private int maxSpeed = 1500;

private bool speedUp = false; //旋轉狀態,加速,減速

// Start is called before the first frame update
void Start()
{
    InvokeRepeating("AdjustSpeed", 0.1f, 0.1f);
}

// Update is called once per frame
void Update()
{
    //旋轉、rotateSpeed 用來控制轉速
    gameObject.transform.Rotate(0, rotateSpeed * Time.deltaTime, 0, Space.Self);

    //點一下,加速,再點一下,減速
    if (Input.GetMouseButtonDown(0))
    {
        speedUp = !speedUp;
    }
}



private void AdjustSpeed()
{
    if (speedUp)
    {
        if (rotateSpeed < maxSpeed)
        {
            rotateSpeed += 10;
        }
    }
    else
    {
        rotateSpeed -= 10;
        rotateSpeed = rotateSpeed < 0 ? 0 : rotateSpeed;
    }

}

相關文章