unity-game-engineanimationanimator

Unity | How to play animations that are stored in a variable (without having them in the animator)


I'm creating a 2D fighting game, where the player can choose from a number of fighters. the fighters have different attacks with different animations.

the data (damage, hitbox, cast time, animation, etc.) for every attack is stored in a Scriptable Object that is then triggered by the player script. this worked out fine so far, but I can't find out how to play an animation from code without needing it to be in the animator.

I've tried several solutions that I found here but they seem to end up having to fall back to putting the animation in the animator.


Solution

  • but I can't find out how to play an animation from code without needing it to be in the animator.

    You will need the Animator-Component to run an animation.

    But you can assign the animation you want to play at runtime. For this you need the RuntimeAnimationController to hold the AnimatorController-object and assign it to your Animator (in code).