【问题标题】:Unity: Infinite While Loop in CoroutineUnity:协程中的无限 While 循环
【发布时间】:2016-10-17 19:32:09
【问题描述】:

好的,我正在尝试为我的 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);
}*/

}

【问题讨论】:

    标签: c# mono unity5 coroutine


    【解决方案1】:

    冲刺你不需要所有这些!

    如果你使用刚体,那么下面的代码就可以了:

    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 类为您的播放器绘制上述组件
    • 根据需要调整参数
    • 你现在应该有一个简单而健壮的dashing :)

    【讨论】:

    【解决方案2】:

    我在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 2012-12-24
      • 2016-07-29
      • 2015-06-25
      • 2013-02-22
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      相关资源
      最近更新 更多