- Home /
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.
Answer by HarshadK · Nov 20, 2014 at 07:22 AM
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
}
}
Ok, so I tried that word for word but it gives me a "No overload for method 'InvokeRepeating' takes '2' arguments" error on the invoke repeating line, what happened and what do I do? Also would I be able to set it so it only starts the timer if it is at a specific angle or range? I have it so my character can go on ramps and angles.
Yea, sorry. InvokeRepeating takes three arguments so that line should be:
InvokeRepeating("FlipCharacter", 5.0f, 5.0f);
Yes, you can invoke the function as and when required but for that you need to invoke the function each time using Invoke. Something like:
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
Invoke("FlipCharacter", 5.0f) //Set this function to invoke after set amount of time
} 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
Invoke("FlipCharacter", 5.0f) //Set this function to invoke after set amount of time
}
}
Similarly you can even set it to trigger anytime you want under any condition you want to check like slopes.
Thanks, one last thing, how would I get the rotation or say, if the rotation is less than 280 but more than 260?
If you are checking the rotation around any axis say in this case around x axis, you do it like:
if(transform.rotation.eulerAngles.x > 260 && transform.rotation.eulerAngles.x < 280)
{
// Do your thing here
}
Answer by Uldeim · Nov 20, 2014 at 07:36 AM
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();
}
}