so i made this chunk of code
IEnumerator dash()
{
float startTime = Time.time;
while (Time.time < startTime + dashingTime)
{
controller.Move(transform.right * dashingPower * Time.deltaTime);
yield return null;
}
}
which is fine but i don’t want this to repeat so try to make in not a coroutine so i just make it a method like this
void dash()
{
float startTime = Time.time;
while (Time.time < startTime + dashingTime)
{
controller.Move(transform.right * dashingPower * Time.deltaTime);
}
}
this for reasons i can not explain completely nukes unity. does anyone have an answer for what i’m doing wrong, what is happening it runs fine as a coroutine but it doesn’t do what i want.
Hello.
You need to understand how IEnumerator works. I will try to explain the basic, the usefull info between IEnumerators and Void functions
IEnumerators are like void functions, they do not repeat themselves (they are usefull for repeating, but you have to command that repeating, they do not repeat automaticly), they are coroutines because they can be “delayed” over time, but they are not automatic in any kind, they are the same as void functions.
The only difference is that you can place yield commands.
This yields allow the function to stop running durint some time/frames, so other part of the code can run while is waiting.
This means, that a while
inside a void function will be looping forever until the condition is not true, and no other code will run, so, the computer will be doing that while
and only that while. This means the cimputer “freezes” until execution go outside the while.
But, in a Ienumerator, if you use a while
and place a yield
command inside the while
, the computer will run the other scripts during yield
, so the game will not “freeze”, only that function will be freezed.