简体   繁体   English

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

[英]How can I fix my characters movement speed

I'm trying to make my first game, but I have a problem with the speed control:我正在尝试制作我的第一款游戏,但速度控制有问题:

Whenever I hold down on the button, everything slows down.每当我按住按钮时,一切都会变慢。 I'm using time.deltatime which I know is global but I can't find any fixes.我正在使用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 is the scale at which time passes. TimeScale 是时间流逝的刻度。 This can be used for slow motion effects.这可用于慢动作效果。 I suggest you to read this: here我建议你阅读这个:这里

Time.timeScale will slow down every game object in your scene. Time.timeScale 会减慢场景中的每个游戏 object。 If you want only one game object to slow down use reduce movespeed of that one gameobject.如果您只想要一个游戏 object 来减慢速度,请使用降低该游戏对象的移动速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM