2016-10-17 31 views
1

好吧,所以我想爲我的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); 
}*/ 

}

回答

0

我忘記了我的while循環中的yield返回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; 
} 
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
  • 調整參數,您的播放器,只要你願意
  • 你應該有一個簡單而強大的瀟灑現在:)
+0

在這種情況下,我需要比衝動更多的控制。我需要能夠隨着時間的推移控制速度。 – Prodigle

+0

然後使用剛體的不同過載和受力模式。您也可以嘗試不斷的強制:https://docs.unity3d.com/Manual/class-ConstantForce.html – Aybe