Hi all,
I'm completely new to Spine and although I have looked at the documentation, set my animations up in Spine and exported successfully to Unity, I'm having trouble getting the animations to work correctly.
I have a very simple state machine, that checks in the update() function for whether the player is moving forward or not, if he is, then the state is set to 'run', if not, the state is set to 'idle'. Easy. As update is called continuously though, I'm not sure how to set the animation to loop, as calling
SetAnimation(0, "run", true);
only plays the first frame of the animation.
I understand why this is happening as it's calling the update (and therefore the SetAnimation) function continuously, but how should it be done correctly so that the animation will loop when in the 'run' and 'idle' states? What is the preferred way to check if we should set the animation if it is not already the one that's playing, or if the animation loop has completed?
Here is some stripped down code:
using UnityEngine;
using System.Collections;
using Spine;
public class PlayerController : MonoBehaviour {
SkeletonAnimation skeletonAnimation;
// States Enum
public enum State {idle, run};
public State state;
void Start () {
skeletonAnimation = GetComponent<SkeletonAnimation>();
// Initialise State To Idle
state = State.idle;
}
void Update () {
// Check If Moving Forward Or Not?
if(Input.GetAxis("Horizontal") > 0) {
state = State.run;
} else {
state = State.idle;
}
// Set State
switch (state) {
case State.idle:
idle ();
break;
case State.run:
run ();
break;
}
}
private void idle () {
// Set Animation To Idle (Looping)
// skeletonAnimation.state.AddAnimation(0, "idle", true, 0);
skeletonAnimation.state.SetAnimation(0, "idle", true);
}
private void run () {
// Set Animation To Run (Looping)
// skeletonAnimation.state.AddAnimation(0, "run", true, 0);
skeletonAnimation.state.SetAnimation(0, "run", true);
}
}
Note, that I have also tried the AddAnimation function, and although it starts with the idle animation looping, and then moves to the looping run animation when the right arrow key is held down, if the right arrow key is released then it remains in the looping run animation instead of returning to the idle animation once again.
I'm sure I'm missing something obvious here, but for the life of me I can't see it. I'd really appreciate some help.