'Character controller dash unity 3D
I was trying to implement dashing mechanic but every time I am trying to run this code my unity freezes. Any solutuions?
if(Input.GetKeyDown(KeyCode.LeftShift)){
StartCoroutine(DashCorutine());
}
}
private IEnumerator DashCorutine()
{
float starttime = Time.time;
while(starttime + dashTime > Time.time){
Vector3 moveDerection= transform.forward*dashlength;
controller.Move(moveDerection *Time.deltaTime*dashSpeed);
}
yield return null;
}
}
Solution 1:[1]
Add a yield inside the while loop.
Solution 2:[2]
You need to add yield break inside while loop
private IEnumerator DashCorutine()
{
float startTime = Time.time;
while (startTime + dashTime > Time.time)
{
Vector3 moveDirection = transform.forward * dashLength;
controller.Move(moveDirection * Time.deltaTime * dashSpeed);
yield break;
}
}
Also check out coding standards for better code quality.
And fix the typos.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BCT |
| Solution 2 | ouflak |
