Yield, WaitForSeconds not working

Hey,

I’m trying to create a camera move. The idea is to have the camera start at a position, wait for 4 seconds, and then move to another position.

What’s happening instead, is the camera is instantly moving to the position without waiting the first 4 seconds, and I’m curious why this is.

Here’s a breakdown of the code;

var camSpeed:           float = 5.0f;
var camStart:			boolean = true;

function Update ()
{
	if (camStart)
   	{
   		CameraStart ();
   		camStart = false;
  	}
}


function CameraStart ()
{
	var camStartPosition = Vector3 (11.5, 0, -5);
	transform.position = camStartPosition;
	
	var getCamSpeed : int = 0;
	getCamSpeed = camSpeed;

	camSpeed = 1.0;
	
	yield WaitForSeconds (4);
		
	var camGamePosition = Vector3 (0, 0,-5);
	transform.position = Vector3.Lerp (transform.position, camGamePosition, Time.deltaTime * camSpeed);
	
	getClock.GetComponent(clock).ResumePlayTime ();
	camSpeed = getCamSpeed;
	
}

2 Answers

2

Because the following two lines will set the position of the camera before you ever get to the yield in CameraStart:

 var camStartPosition = Vector3 (11.5, 0, -5);
 transform.position = camStartPosition;

Also, your lerp will not work because after one iteration through CameraStart(), the camStart variable will no longer be true, so it won’t get called again.

That's actually expected. That is the position I wish the camera to start at, wait for 4 seconds and then move to the position I want it to end at. What's happening is the camera doesn't wait for the 4 seconds. It just instantly moves from that position to the end position. It's like Yield WaitForSeconds (4) isn't even there.

Ah - your question wasn't very clear in that respect. What I see (once I remove the getClock bit from your code) is the camera jump to (11.5, 0, -5), and then 4 seconds later jump to (11.12, 0, -5). If you're wondering why the lerp() isnt' working - that's because it needs to be called repeatedly over a series of frames in order to create a smooth transition - you're only calling it the once.

Heh, sorry for not be clear it's very early in the morning right now. actually the Lerp transition seems to work fine. It's just not waiting before the Lerp call. yield WaitForSeconds (4); is being ignored for some reason.

With that code? How odd. Sorry - I can neither reproduce nor explain that behaviour!

Well then that means some other code is forcing the camera movement. At least that helps me narrow it down! thanks for the help!

The problem was a separate script was arguing with the camera movement. I have located the problem and fixed the issue.

Thanks to tan for testing my script and assuring me that it works!