- Home /
Yield WaitForSeconds not working > once in coroutine
Hey guys!
I am having an issue with a bit of code that was working fine before, but decided to not work 2 days before the project it's for is due.
What I wanted was the camera to Lerp from one point to another taking a certain amount of time. It was then to wait there for a certain amount of time, then go back to it's original position.
here is what the coroutine looks like:
IEnumerator cameraMove (){
duration = durationToEnd;
fromPos = startAt;
toPos = endAt;
// Remove action from character
yield return new WaitForEndOfFrame();
controller.GetComponent<SoulInfo>().canAct = false;
// Enable Cinematic override to prevent camera following
// Player
mainCamera.GetComponent<WorldControls>().cinematicOverride = true;
activated = true;
yield return new WaitForSeconds(duration + durationToWait);
duration = durationToStart;
fromPos = endAt;
toPos = startAt;
yield return new WaitForSeconds(duration);
activated = false;
// Re-enable Cinematic override
mainCamera.GetComponent<WorldControls>().cinematicOverride = false;
// Return action to character
controller.GetComponent<SoulInfo>().canAct = true;
}
And here is how I am calling it in the Update:
// Update is called once per frame
void Update () {
if(activated){
//Interpolate from one point to another
mainCamera.transform.position = Vector3.Lerp(fromPos, toPos, Time.time/duration);
}
}
It seems to make perfectly logical sense to me, but does not behave how I'd like.
It DOES move from start to end absolutely fine at the right speed and DOES wait the right amount of seconds at that point.
It DOES NOT get past the second WaitForSeconds()
I tested it with Debug.Log and it stops working as soon as add:
duration = durationToStart;
fromPos = endAt;
toPos = startAt;
before the second WaitForSeconds.
Anyone know what's going on?
What is the value of durationToStart? And have you verified that value with Debug.Log?
durationToStart is a public variable that denotes the length of time it takes to get from the end point to the start point
I have Debugged all duration values and even tried putting other values into the 2nd waitForSeconds to no avail
It seems to dislike doing a second WaitForSeconds after I have assigned values to my variables
UPDATE:
Sorry I had not actually tried a literal float
Once I truied a literal float it worked fine
Which is extremely odd. It seems you were definitely on to something with the duration to end var
It shouldn't make a difference if the value of durationToStart is what you expect. Add the Debug.Log right after you set duration and show the value of duration. Since you're dealing with co-routines, maybe something is altering durationToStart unexpectedly.
I do not get how you use the Lerp function. You could have a look there for explanation http://unitygems.com/mistakes1/#lerp .Also, why don't I see the function being called in the Update? I do not see camera$$anonymous$$ove in the update. Just for the final, why don't you make an animation? If I got it right you have your camera moving from A to B and then from B to A. You could have one animation and the same backwards.
Answer by astorga · Nov 06, 2012 at 01:21 PM
Why don't you use iTween?
You can do the movement you want in one code line:
iTween.MoveTo(camera.gameObject, destPos, moveTime);
Thanks for that I would like to continue this method before importing other scripts though, but I know where to go as a last resort now