- Home /
WaitForSeconds not working at all C#
I have a class file that zooms the camera for aiming. I also have a zoom animation for my gun. What i want to do is wait for the gun to catch up some before i do the camera zoom. It zooms by the right mouse button down event
However, it just doesnt seem to do anything like the function is not being called. I have looked at numerous examples and i dont see what i am doing wrong?
UPDATE: I got it fixed. I will revise my code to reflect that incase anyone else has problems down the road.
void Update () {
StartCoroutine("fieldOfView");
}
IEnumerator fieldOfView(){ // Not IEnumerable
if(Input.GetMouseButtonDown(1)){
aim = true;
ready = false;
}
if(Input.GetMouseButtonUp(1)){
aim = false;
}
if(aim){
yield return new WaitForSeconds(2);
cam.camera.fieldOfView = Mathf.Lerp(cam.camera.fieldOfView, distance, speed * Time.deltaTime);
}
if(!aim){
cam.camera.fieldOfView = Mathf.Lerp(cam.camera.fieldOfView, original_position, speed * Time.deltaTime);
}
}
Every example i have tried has not worked.
Answer by lvictorino · Aug 27, 2012 at 07:28 AM
It seems to actually work perfectly in your code.
Starting a coroutine means "executing another piece of code at the same time that your current piece of code". So you're just "waiting" but this does only pause the coroutine code... not the main "thread".
Even if it doesn't seem to wait 5 seconds it actually does, but not in your main execution thread that's why it seems buggy for you.
I would suggest something using a boolean to control your flow. Something like this:
void Update ()
{
if ( ready == false ) // check if you're no in the 5s delay
{
return;
}
if(Input.GetMouseButtonDown(1))
{
aim = true;
ready_to_anim = false; // set the boolean to false == your not allowed to do something for 5s
StartCoroutine("wait");
return;
}
// rest of your code
}
IEnumerator wait()
{
yield return new WaitForSeconds(5);
ready_to_anim = true; // delay is over now you can do something
}
I think what i am going to try to do is throw the ifs statements in a function and then just call that function in update. Then, i could pause that function?
Early exits can produce cleaner code, but only if it's possible to use them. In this case an early exit doesn't make any sense. It will ignore all other code in Update as long as "ready" is false.
I guess the trigger condition should be something like:
if(ready && Input.Get$$anonymous$$ouseButtonDown(1))
I'm not sure what the delay should do since it has no function in the question code. It would help to know what's the desired behaviour.
Bunny - i think that would work however i moved the code inside a function and its working now.
Answer by Ashkan_gc · Aug 27, 2012 at 07:35 AM
I did not read the code carefully to trace it and see what in each state will happen but your coroutine is returning IEnumerable instead of IEnumerator and it should be the reason for it to not work.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How do i start a game with Unity, what do i need first? 0 Answers
Not getting component properly 1 Answer