Flipping character after set time upside-down

Ok, so I’m trying to put a feature into my game which automatically flips the character after a period (2-5 seconds) of them being upside down, I’m guessing that I’d have to use coroutine, but I’m not sure how. And I’m also not sure how to get the script to check if the character is upside down at the time. If anyone has an annotated script which I can try to understand and learn from that’d be great also.

Rather than coroutines you can use InvokeRepeating if you want to flip character after a fixed interval of time. Just use a boolean that you set as per the state of the character which you can check to decide.

bool isUpsideDown = false; // We consider he starts straight up.

void Start()
{
    InvokeRepeating("FlipCharacter", 5.0f);  // 5.0f can be replaced with any time duration you want
}

void FlipCharacter()
{
    if(isUpsideDown) {
        // Rotate the character to be straight up. You can simply change its rotation or can apply smoothing to flip and all that stuff.
        isUpsideDown = false; // Set bool to set the current state of character
    } else {
        // Rotate the character to be upside down. Again you can do it simply or use fancy stuff
        isUpsideDown = true; // Set bool to set the current state of character
    }
}

When your character gets flipped, use
StartCoroutine("FlipTimer");

If, at some point before it’s done, the character gets unflipped, you can then use
StopCoroutine("FlipTimer");

Because you’re using the version of StartCoroutine with a string argument, you can then stop the routine before it completes. In the coroutine itself, you’ll want to yield a WaitForSeconds and check at the very end that the character is flipped (timing issues are a pain, so always double-check). eg.

IEnumerator FlipTimer()
{
   yield return new WaitForSeconds(5.0f);
   if (character.isFlipped)
   {
      UnflipCharacter();
   }
}