- Home /
Coroutine troubles
Hi,
I'm hoping someone can help me out with this problem. I'm trying to get my GameObject to move along a path, which is stored in an array. The GameObject moves along the path correctly but I'm trying to get the game to wait for the GameObject to finish moving before anything else can be done.
In addition, when I query the value of the busy variable after the line StartCoroutine(moveAlongPath()); busy returns as True, when it should be False. I understand that this is because of the nature of coroutines, they run alongside the current process, but I want to get the value of busy after the movement is complete. If I remove the coroutine of moveAlongPath unity crashes.
I'm using coroutines to smoothly move my GameObject.
Here's the code. Thanks in advance.
#pragma strict
var busy: boolean = false;
var path: Array;
function moveLinear(goal: Vector3, rateOfMovement: float){
var startPos: Vector3;
while(true)
{
busy = true;
startPos = transform.position;
if (startPos == goal)
{
busy = false;
break;
}
transform.position = Vector3.MoveTowards(startPos, goal, Time.deltaTime*rateOfMovement);
yield;
}
}
function moveAlongPath () {
var i: int = 0;
while (i < path.length)
{
if (!busy)
{
Debug.Log(i);
Debug.Log(busy);
var goal: Vector3 = path[i];
StartCoroutine(moveLinear(Vector3(goal.x, 0, goal.z), 10));
i++;
}
yield;
}
}
function Start () {
path = new Array ();
path = [Vector3(10,0,0),Vector3(20,20,0),Vector3(30,0,0),Vector3(40,40,0)];
}
function Update () {
if (Input.GetMouseButtonDown (0))
{
StartCoroutine(moveAlongPath());
Debug.Log('b '+busy);
}
}
dude you can't really go "startpos == goal"
you know it's like comparing floats. (search 100s of questions on it here)
more likely you would do something like sqr$$anonymous$$agnitude less than some small value, or use $$anonymous$$ath.Approximately, or find if you're inside a small AABB or whatever
in a general sense your routine "moveLinear" is a coroutine, it does something, it skips frames while it is doing it, and then, it returns.
so in fact you can very simply write code like:
yield moveLinear();
and that will just wait until your "moveLinear" finishes.
FWIW i would write that routine more like this
function moveLinear
{
busy = true
while ( sqrDistance > 5 centimeters )
{
move a little ..
yield
}
set position to exactly the desired position
busy = false
}
Answer by Fattie · Mar 30, 2013 at 12:10 PM
"In addition, when I query the value of the busy variable after the line StartCoroutine(moveAlongPath()); busy returns as True, when it should be False"
no, it should be TRUE.
if you YIELDED to moveLinear it would "wait" there, and on the next line it would be FALSE
But you are not yielding to it. You're just telling it to run in the "background". so it is imemdiately TRUE and stays TRUE for a few seconds or however long it will take to run.
Yes, of course. Thanks for clearing that up.
yield moveLinear() works a treat and I removed the coroutine of moveAlongPath().
I realized something else. I was querying the value of busy from the update function directly after calling moveAlongPath(). This returns the value of busy immediately, as moveAlongPath() is running. The solution of getting the value of busy after moveAlongPath() was to move this query into moveAlongPath(), at the end of the function. Doing so returns false as moveAlongPath() needs to wait for moveLinear() to complete first.
i only wish i could help more!
here's a very handy trick. just use InvokeRepeating for simple timers in Unity. So, try this.
in Awake or whatever ... InvokeRepeating("TESTE", 0.20 );
then add a simple routine
function TESTE()
{
Debug.Log("the boolean is currently ... "+busy);
}
you will see this is incredibly useful. watch your console and it will give you a good report on what the hell is happening!
it took me years to understand coroutines as little as I do, by asking the experts on this list over and over
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
MoveTowards inside Coroutine 2 Answers
Moving only if straight path dijkstra 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
How to make a 2D object move smoothly in a random direction for a set amount of time. 2 Answers