private IEnumerator FlyBehaviors() { if (transform.position.y < 1) // if we're on the ground, take off { dragon.TakeOff(); yield return(new WaitForSeconds(5)); // time the animation takes } if (Random.Range(0, 4) == 2) { dragon.FlyFlameAttack(); yield return(new WaitForSeconds(3)); } bool FlyingUp = FaceRandomSkyDirectionAndReturnTrueIfFlyingUp(); if (FlyingUp) { dragon.FlyForward(); float FlyTime = Random.Range(10, 30); float FlyTimeSoFar = 0; while (true) { yield return(new WaitForEndOfFrame()); transform.position += (transform.forward * FlyForwardSpeed * Time.deltaTime); FlyTimeSoFar += Time.deltaTime; if (FlyTimeSoFar >= FlyTime) { State = DragonState.flying; yield return(null); yield break; } } } else { float TimeGliding = 0; dragon.FlyGlide(); while (true) { yield return(new WaitForEndOfFrame()); transform.position += (transform.forward * FlyDownSpeed * Time.deltaTime); TimeGliding += Time.deltaTime; if (TimeGliding > 3f) // time the animation takes { dragon.FlyGlide(); TimeGliding = 0; } if (transform.position.y <= HeightToStartLandingAt) { transform.position = new Vector3(transform.position.x, 0, transform.position.z); transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z); dragon.Land(); yield return(new WaitForSeconds(4)); // time the animation takes State = (DragonState)Random.Range(0, 5); yield return(null); yield break; // is this needed to stop the routine?? } } } }