Hey. I was wondering if there was a way to stop a looping animation. It starts out just fine, but even when the if statement isn't met, the animation continues to play. Why is this? Any help would be greatly appreciated.
using UnityEngine;
using System.Collections;
public class BoostAndDodge : MonoBehaviour {
tk2dSpriteAnimator anim;
public float boostSpeed = 5;
public float chargeTimer = 1;
public float waitTimer = 1;
bool executing = false;
IEnumerator WaitForCharge()
{
executing = true;
yield return new WaitForSeconds(chargeTimer);
Energy.ENERGY = 1;
Debug.Log("coroutine complete");
executing = false;
}
IEnumerator Wait()
{
yield return new WaitForSeconds(waitTimer);
Energy.ENERGY = 0;
}
// Use this for initialization
void Start () {
anim = GetComponent<tk2dSpriteAnimator>();
}
// Update is called once per frame
void Update () {
float boost = boostSpeed * Time.deltaTime;
if(Energy.ENERGY == 1 && Input.GetButton("Jump"))
{
anim.Play("Boost");
transform.Translate(new Vector3(boost, 0, 0));
StartCoroutine(Wait());
Debug.Log("boosted");
}
if(executing == false && Energy.ENERGY == 0)
{
StartCoroutine(WaitForCharge());
}
}
}