using UnityEngine;
using System.Collections;
public class walkAnimator : MonoBehaviour {
public string characterStatus;
public int animationStatus;
public float idleFPS;
private float idleSecondsToWait;
public bool idleLoop;
public Texture[] idleFrames;
private int idleCurrentFrame;
public float walkFPS;
private float walkSecondsToWait;
public bool walkLoop;
public Texture[] walkFrames;
private int walkCurrentFrame;
// Use this for initialization
void Start ()
{
idleCurrentFrame = 0;
idleSecondsToWait = 1/idleFPS;
walkCurrentFrame = 0;
walkSecondsToWait = 1/walkFPS;
characterStatus = "Walk";
animationStatus = 1;
}
void Update ()
{
if (animationStatus == 1)
{
if (characterStatus== "Idle")
{
StartCoroutine(idle());
}
if (characterStatus== "Walk")
{
StartCoroutine(walk());
}
}
if (animationStatus ==2)
{
}
}
IEnumerator idle()
{
animationStatus = 2;
bool stop = false;
if(idleCurrentFrame >= idleFrames.Length)
{
if(idleLoop == false)
stop = true;
else
idleCurrentFrame = 0;
Debug.Log("loop");
}
yield return new WaitForSeconds(idleSecondsToWait);
renderer.material.mainTexture = idleFrames[idleCurrentFrame];
idleCurrentFrame++;
if(stop == false)
StartCoroutine(idle());
if (characterStatus== "Walk")
{
StartCoroutine(walk());
StopCoroutine("idle");
}
}
IEnumerator walk()
{
bool stop = false;
animationStatus = 2;
if(walkCurrentFrame >= walkFrames.Length)
{
if(walkLoop == false)
stop = true;
else
walkCurrentFrame = 0;
Debug.Log("loop");
}
yield return new WaitForSeconds(walkSecondsToWait);
renderer.material.mainTexture = walkFrames[walkCurrentFrame];
walkCurrentFrame++;
if(stop == false)
StartCoroutine(walk());
if (characterStatus== "Idle")
{
StartCoroutine(idle());
StopCoroutine("walk");
}
}
}
My issue seems to come after the initial load. The script passes into the walk coroutine with no trouble but if I chance the characterStatus string to walk in an attempt to swap animations the script starts stacking the coroutine over and over instead of running through it in a loop.