Unity:协程中的无限循环

Unity: Infinite While Loop in Coroutine

好的,我正在尝试为我的 2d 角色创建一个小破折号协程。当协程调用时,重力关闭,他会随着时间的推移在 2 种速度之间徘徊。问题出在我的 Dash 协程中,while 循环检查何时 time.time(current time) > start time + dash duration.

在使用 Mono 调试时,我发现我的变量 currentTime 在设置后没有改变,即使我可以清楚地看到 while 循环 运行 不止一次。这让我陷入无限循环。

有什么建议吗?

    void Update () {
    MoveAndJump ();
    CheckDash ();
    }




void MoveAndJump(){

    if(dashing){
        return;
    }

    Vector2 moveDir = new Vector2 (Input.GetAxisRaw ("Horizontal") * moveSpeed, rb.velocity.y);
    rb.velocity = moveDir;

    // Consider Switching to an overlap circle based on the actual character graphic. This currently uses a rectangle
    // This can also use 4 points to create a more complex box shape. This only works well when using at least about 1 unit size. any smaller and it is innacurate
    isGrounded = Physics2D.OverlapArea (groundPoint_right.position, groundPoint_left.position, groundMask);
    Debug.Log (isGrounded);

    if (Input.GetAxisRaw ("Horizontal") == 1) {
        transform.localScale = new Vector3 (1 , 1 * transform.localScale.y, 1* transform.localScale.z);
    } else if (Input.GetAxisRaw ("Horizontal") == -1) {
        transform.localScale = new Vector3 (-1, 1* transform.localScale.y, 1* transform.localScale.z);
    }


    if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
        rb.AddForce (new Vector2 (0, jumpHeight));
    }
}


void CheckDash(){
    if(Input.GetKeyDown(KeyCode.W) && !dashing && Time.time > nextdashtime){
        dashing = true;
        StartCoroutine ("Dash");
    }
}

IEnumerator Dash(){
    //Before dash loop
    rb.gravityScale = 0f;
    float startDashTime = Time.time;
    float endDashTime = startDashTime + dashDuration;
    float currentDashTime;
    //Dash Loop
    while(Time.time < (startDashTime + dashDuration)){
         currentDashTime = Time.time;
        // The value to lerp by should be the % that time.time is between start time and end time
        rb.velocity = new Vector2 (Mathf.Lerp (startDashSpeed, endDashSpeed, ((currentDashTime - startDashTime) / (endDashTime - startDashTime))),0f);
    }

    //When dash loop is complete
    nextdashtime = Time.time + dashcooldown;
    dashing = false;
    rb.gravityScale = 1;
    yield return null;
}



// FOR CHECKING GROUND CHECK LIMITS
/*void OnDrawGizmos(){
    Gizmos.color = Color.red;
    Gizmos.DrawLine (groundPoint_left.position, groundPoint_right.position);
}*/

}

潇洒不需要这些!

如果您使用刚体,则可以使用以下代码:

using UnityEngine;

public class CubeController : MonoBehaviour
{
    private Rigidbody _rigidbody;
    public float Force = 10;

    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        ForceMode? forceMode = null;

        if (Input.GetKeyDown(KeyCode.W)) // normal
            forceMode = ForceMode.Force;
        else if (Input.GetKeyDown(KeyCode.S)) // dash
            forceMode = ForceMode.Impulse;

        if (forceMode.HasValue)
            _rigidbody.AddRelativeForce(Vector3.forward*Force, forceMode.Value);
    }
}

这是一个使用 3D 的示例,但您也有相同的 2D 示例:https://docs.unity3d.com/ScriptReference/Rigidbody2D.html

步骤:

  • 将刚体组件添加到您的地面,将其设置为运动学
  • 为你的播放器添加刚体组件
  • 使用 Rigidbody2D class
  • 为您的播放器绘制上述组件
  • 根据需要调整参数
  • 你现在应该有一个简单而强大的 潇洒 :)

我忘记了 while 循环中的 yield return null 使我陷入无限循环。现在工作正常。

 while(Time.time < (startDashTime + dashDuration)){
     currentDashTime = Time.time;
    // The value to lerp by should be the % that time.time is between start time and end time
    rb.velocity = new Vector2 (Mathf.Lerp (startDashSpeed, endDashSpeed, ((currentDashTime - startDashTime) / (endDashTime - startDashTime))),0f);
    yield return null;
}