【发布时间】:2021-03-08 10:38:32
【问题描述】:
我正在尝试制作我的第一款游戏,但速度控制有问题:
每当我按住按钮时,一切都会变慢。我正在使用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);
}
}
【问题讨论】:
-
如果你想让一个物体更快,你给这个物体的速度乘以它。如果你想让它变慢,将它乘以一个小于 1 的数字,如果你的对象应该有一半的速度,那么
self.velocity = new Vector2(moveSpeed*0.5f, 0f); -
另外,您可能应该使用
Rigidbody.AddForce而不是直接设置速度
标签: c# visual-studio unity3d qvector3d