2
\$\begingroup\$

I want an object to rotate after a delay. I tried using StartCoroutine, yield and WaitForSeconds, but failed. How can I do it?

\$\endgroup\$
  • \$\begingroup\$ If you have additional questions, you should ask them as new questions, instead of editing one you've previously asked. \$\endgroup\$ – Problematic Nov 6 '15 at 21:05
3
\$\begingroup\$

You could create a variable for example called timer of type float and make it equal to the amount of seconds you would like to wait, then in the Update() function subtract Time.deltaTime from your timer variable. Afterwards use if statement to check if the timer variable is equal to zero, if so rotate the object.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I do not get errors anymore, but it still has no effect \$\endgroup\$ – That's me. Aug 27 '15 at 20:10
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – MichaelHouse Aug 28 '15 at 0:35
2
\$\begingroup\$

Multiple choices, but you will have to use Coroutine sometimes, so try to figure out why its not working here.

My tips : careful to not call StartCoroutine in Update() without protection.

Exemple:

private bool enter = false;

Update() {
    if (enter == false)
        StartCoroutine(your_timer());
}

IEnumerator your_timer() {
    enter = true;
    Debug.Log("Your enter Coroutine at" + Time.time);
    yield return new WaitForSeconds(2.0f);
    enter = false;
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

From here

public void Invoke(string methodName, float time);

Invokes the method methodName in time seconds.

There is a restriction that the method needs to take no parameters and return void, so you should wrap your rotation in another function, like so:

void delayedRotation() {
    transform.Rotate (new Vector3 (0, 0, 1) * Time.deltaTime);
}

and then adjust your code:

if (isRolling) 
{
    transform.position += new Vector3 (0,1,2)
    Invoke("delayedRotation", 0.5f);
} 
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$
void Update(){
    if(INSERT_CONDITION_HERE){


        StartCoroutine(Timer);
     }
}

Then

IEnumerator Timer(){
    yield return new WaitForSeconds(Time);
    CODEGOESHERE
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.