0

我正在尝试制作我的第一款游戏,但速度控制有问题:

每当我按住按钮时,一切都会变慢。我正在使用time.deltatime我知道是全球性的,但我找不到任何修复程序。

void Start ()
{
    self = GetComponent<Rigidbody2D>();
    InvokeRepeating("SwitchDirections", switchTime, switchTime * 2);
    StartCoroutine(SpawnBallLoop());
    StartCoroutine(Count());
}

IEnumerator SpawnBallLoop ()
{
    while (gameOver == false)
    {
        yield return new WaitForSecondsRealtime(2f);
        SpawnBall();
    }
}

void SpawnBall ()
{
    Instantiate(ball, new Vector3(Random.Range(-2.18f, 2.18f), 4.6f, 0f), Quaternion.identity);
}

void UpdateClock ()
{
    seconds += 1;
    clock.text = "Time: " + seconds;
}

void SwitchDirections ()
{
    moveSpeed *= -1;
}

void FixedUpdate ()
{
    self.velocity = new Vector2(moveSpeed, 0f);
    if (Input.GetMouseButton(0))
    {
        Time.timeScale = 0.5f;
    }
    else
    {
        Time.timeScale = 1f;
    }
}

IEnumerator Count ()
{
    while (gameOver == false)
    {
        yield return new WaitForSecondsRealtime(1);
        UpdateClock();
    }
}

public void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.transform.tag == "Ball")
    {
        GetComponent<SpriteRenderer>().enabled = false;
        transform.GetChild(0).gameObject.SetActive(true);
    }
}
4

2 回答 2

0

Time.timeScale 将减慢场景中的每个游戏对象。如果您只想让一个游戏对象减速,请使用降低该游戏对象的移动速度。

于 2021-03-08T17:18:15.223 回答
0
  Time.timeScale = 0.5f;

TimeScale 是时间流逝的刻度。这可用于慢动作效果。我建议你阅读这个:这里

于 2021-03-08T10:54:29.653 回答