团结:在Coroutine中无限循环

时间:2016-10-17 19:32:10

标签: c# mono unity5 coroutine

好的,所以我正在尝试为我的2D角色创建一个小破折号协同程序。当协程调用时,重力关闭,他在一段时间内在两个速度之间徘徊。问题出在我的Dash协程中,while循环检查time.time(当前时间)>开始时间+短跑持续时间。

在用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);
}*/

}

2 个答案:

答案 0 :(得分:0)

您不需要划线的所有内容!

如果您使用刚体,则以下代码将执行:

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
  • 为您的播放器绘制上述组件
  • 根据您的喜好调整参数
  • 你现在应该有一个简单而强大的划线:)

答案 1 :(得分:0)

I forgot a yield return null within my while loop that stuck me in an infinite loop. Works fine now.

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