I think in the Unity 5 previews they said they’ll incorporate something that improves this, but if I’m impatient, what can I do right now?
Say I’m doing tile-based motion like a board game, so I have an animation clip that moves a piece 1 square to the left, an animation clip that moves it 1 square to the right, etc. What’s the simplest way to make it so when the animation clip completes, it adds that offset to its position before starting the next animation clip?
I’ve tried this out, it feels very klugey but it appears to work:
public Animator anim;
public Transform gamePiece;
....
public void stepComplete() {
StartCoroutine(UpdateOffset());
}
IEnumerator UpdateOffset() {
Vector3 tempHold = gamePiece.position;
anim.Play("Neutral");
yield return new WaitForEndOfFrame ();
yield return new WaitForEndOfFrame ();
transform.position= tempHold;
}
Neutral is an animation state that returns gamePiece to the 0,0,0 position. All the other animations animate gamePiece to the next square over and then call stepComplete as an Animation Event.
I’m calling WaitForEndOfFrame twice, just as the result of experimenting until I found something that didn’t flicker in the game window.
When using this in the editor, the piece will flicker in the Scene window. Presumably it’s refreshing at a different rate than the game window.
Instead of the yields would I be better off putting an Animation Event in the first frame of the Neutral animation that assigns the reposition? Maybe that would be better. Or is there some third option that’s simpler?
Okay, I tried the version where I called a different function from the start of the Neutral timeline, but I still needed to call one WaitForEndOfFrame to keep it from flickering.
Now I can play them without the WaitForEndOfFrame, plus I can use the Animation component instead of the Animator component and skip the hassle of turning off transitions which I didn’t want in this case anyways.