greyspace
greyspace

Reputation: 545

Is it possible to use code to swap an object's animator-based animation?

I have a 2D game, and have a very simple looping 4-frame animation (using Animator) on an object. This object, Snake, also holds some scripts I need to keep (with Snake's stats, attacks, etc). I'd like to swap this 4-frame animation with a different but extremely similar 4-frame animation using code, but I don't know if that's possible.

I have been reading conflicting reports on this (because I don't know exactly where to look) and I don't want to get super far into this if it's a dead-end.

Edit: As I understand it, the controller the object currently uses is fine, but I'd like to swap it's .anim file? Or the sprite sheet that the .anim file uses.

Upvotes: 0

Views: 89

Answers (1)

Vladimir Beletskiy
Vladimir Beletskiy

Reputation: 328

Create a new state with a similar animation. Then you can switch the animation simply by

float transitionTime = 0;
animator.CrossFade("StateName", transitionTime);

There is another option: you could animate an abstract value, and then use it in your custom code to load sprites, depending on your conditions. For example, you could animate an int value and access a parameter which stores sprites in an array.

Upvotes: 1

Related Questions