function Start(){
StartCoroutine("MoveForward", walkSpeed);
}
function MoveForward(speed : float){
while (true) {
var forward = transform.TransformDirection(Vector3.forward);
direction = forward * speed;
characterController.SimpleMove(direction);
yield;
}
}
But it doesn’t move my object forward, any ideas whats wrong?
For my personal culture, what is a coroutine exactly ?
A script that is executed each frame too, like Update ? But since you are already using a while loop with a yield, isn’t calling the function normally (MoveForward (walkSpeed) giving the same effect ?
Any function containing the ‘yield’ statement is a coroutine. They are functions that don’t execute all at once, but stop executing whenever a yield is encountered, to continue at a later time. When yield is used by itself (as the original poster uses it) the function is stopped for exactly one frame and then continues where it left off. When yield is used in combination with other conditions, like for example WaitForSeconds or a WWW class, the function is continued when the condition is met (until the WaitForSeconds time has expired or the WWW request completed).
Ok…but why bothering calling it with a function (StartCoroutine) ? Does it have something special ?
My only hypothesis is that it can be stopped by the function StopCoroutine, whereas calling normally the function would require an exit condition, for the loop. Am I right ?