using coroutines
Why does my coroutine BikeEnds() doesnt execute after the coroutine JetPackEnds()? so here's the scenario, if the player collides to (pdestroyed2 == true)jetpack powerup, the jetpack animation plays, and while having the power up and suddenly the power up (pdestroyed == true)Bike appears, the animation of Bike doesnt shows. .though the moveSpeed is changed to 7f, how could i fix this? i am new to programming as well as in unity. . .heres my code
void Update()
{
if(pdestroyed == true)//&& pdestroyed2 != "yes")
{
StartCoroutine(BikeEnds());
}
if(pdestroyed2 == true)//&& pdestroyed2 != "yes")
{
StartCoroutine(JetPackEnds());
}
}
IEnumerator BikeEnds()
{
if (pdestroyed == true && pdestroyed2 == false)
{
moveSpeed = 7f;
myAnimator.SetBool ("Bike", true);
yield return new WaitForSeconds (5.0f);
myAnimator.SetBool ("Bike", false);
pdestroyed = false;
}
}
IEnumerator JetPackEnds()
{
if (pdestroyed2 == true && pdestroyed == false)
{
dblJump = true;
myAnimator.SetBool("JetPack",true);
yield return new WaitForSeconds (5.0f);
myAnimator.SetBool ("JetPack", false);
pdestroyed2 = false;
dblJump = false;
}
}
Answer by Jordi-Bonastre · Apr 28, 2016 at 04:08 PM
Have a look to my proposal. I've used a Coroutine that calls to the other Coroutines. With this workaround, you can remove your conditionals inside the Coroutines and everything works fine.
using UnityEngine;
using System.Collections;
public class testCoroutine : MonoBehaviour {
public bool launchCoroutines = false;
bool pdestroyed = true;
bool pdestroyed2 = true;
void Update()
{
if (launchCoroutines) {
launchCoroutines = false;
StartCoroutine (MyCoroutines ());
}
}
IEnumerator MyCoroutines(){
if(pdestroyed)
yield return StartCoroutine(BikeEnds());
if(pdestroyed2)
yield return StartCoroutine(JetPackEnds());
yield return null;
}
IEnumerator BikeEnds()
{
Debug.Log ("BikeEnds");
//moveSpeed = 7f;
//myAnimator.SetBool ("Bike", true);
yield return new WaitForSeconds (5.0f);
//myAnimator.SetBool ("Bike", false);
pdestroyed = false;
}
IEnumerator JetPackEnds()
{
Debug.Log ("JetPackEnds");
//dblJump = true;
//myAnimator.SetBool("JetPack",true);
yield return new WaitForSeconds (5.0f);
//myAnimator.SetBool ("JetPack", false);
//dblJump = false;
pdestroyed2 = false;
}
}
Your answer
Follow this Question
Related Questions
OnCollisionEnter2D message being ignored 1 Answer
(2D) Make a GameObject Shake 0 Answers
Delay Animator Action in C#? 0 Answers
Logic for a lot of animations 0 Answers