Hi, guys.

I’ve never used coroutines before but when i asked for a solution to an event problem, some of you told me that’s the best way to deal with events that trigger when some requisites are met.

My scenario is this:

I have a car running along a path (circuit) by placing it into a specific percent of this path (i use awesome tool iTween). Every 10 seconds of game a random event occurs and this event may involve a wheel puncture. When this happens i use a flag (boxes==true) so when the car reaches tue boxes road entrance it will be relocated on this other path (boxes). So i use this code:

```
if (boxes==true && percent>=boxes_entrance){ // Coroutine needed
if (percentbox>=0.5 && timer<=FixCarTimeLapse) { //It waits for reparation on the box (0.5 is the middle of box lane)
timer+=Time.deltaTime;
wheelsPoints=100;
}
else percentbox=percentbox+Time.deltaTime/Circuit_DriveThrough;
if (percentbox>=0.4f){ //controls lap counter when car passes boxes (Coroutine needed)
totalLaps++;
}
iTween.PutOnPath(gameObject, iTweenPath.GetPath("boxes"), percentbox);
if (percentbox>=1) { //exits boxes road
boxes=false;
percentbox=0;
timer=0;
percent=boxes_exit;
TimePerLap=InitialTimePerLap/3;
}
}
```

Where:

percent: is the position of the car, a float between 0 and 1 for the function iTween.PutOnPath() which uses percent of the path as its last argument.

percentbox: is the position of the car when it’s driving along boxes lane.

Circuit_DriveThrough: is a constant defined in the circuit and it describes the time lapse for a car to drive the whole boxes lane.

boxes_entrance and boxes_exit: are the exact percent of path circuit where it “joins” the start of boxes path and the end of boxes path, respectively.

FixTimeCarElapse: is a variable of the mechanic team, it defines the time the team will last fixing the car.

So, the problem in this code is in the lines commented as “coroutine needed”. The first line of code is a problem for me when the car breaks after reaching boxes entrance but before it finishes the circuit: the car “jumps” from its position to boxes_entrance point. That jump may be very large. As percent is a float i cant use:

if (boxes==true && percent==boxes_entrance)

because percent will vary from frame to frame so it wont be exactly the same as boxes_entrance. I may use a “distance” here:

if (boxes==true && percent>=boxes_entrance && percent=0.4f)

0.4 is the percent of boxes lane that matches a lap (when a car passes boxes lane it counts as a lap). But i cant use the same solution as before because it will count more than 1 lap. I cant calculate the exact time a frame will delay so a solution like:

if (percentbox>=0.4 && percentbox<0.40001)

will mean lots of lap counts.

To post it clearly: I need a way to manage events that happen once and these events are triggered by float variables which grow ratio we dont know.

I’ve been checking coroutines and i understand how they work, but i cant “see” the idea clearly. How would you solve this problem using coroutines? Thank you in advance.