我如何修复我的角色移动速度



我正在尝试制作我的第一款游戏,但我在速度控制方面遇到了问题:

每当我按下按钮,一切都变慢了。我正在使用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);
}
}
Time.timeScale = 0.5f;

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

时间。timeScale将减慢场景中的每个游戏对象。如果你只想让一个游戏对象变慢,那就降低那个游戏对象的移动速度。

最新更新