Итак, я пытаюсь создать небольшую копию для моего 2-го символа. Когда вызов coroutine, сила тяжести отключается, он задерживается между 2 скоростями за время. Проблема заключается в моей косой черточке, цикл while проверяет, когда time.time (текущее время)> время начала + длительность тире.Единство: бесконечное время цикла в Coroutine
При отладке этого с помощью 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);
}*/
}
Мне нужно больше контроля, чем импульс в этой ситуации. Мне нужно иметь возможность контролировать скорость с течением времени. – Prodigle
Затем используйте различные режимы перегрузки и силы жесткого тела. Вы также можете попробовать постоянную силу: https://docs.unity3d.com/Manual/class-ConstantForce.html – Aybe