Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Best option for 2D Animations ?

Discussion in 'Animation' started by frankdervoelker, Mar 29, 2023.

  1. frankdervoelker

    frankdervoelker

    Joined:
    Nov 16, 2020
    Posts:
    11
    Hey guys. I am making a game with quite complex 2D animations and see that I have multiple options. What would be the best regarding ease and performance?

    1) Use the animator and state machine: This is the most obvious one but it is becoming quite complex and confusing having 50 states and all states go into each other somehow. Also doing this for every mob/enemy again and again is very complex and frustrating.

    2) Using animator but changing animations programatically with GetComponent<Animator>().Play("Jump")

    3) (my preferred option)Using SpriteRenderer directly and swapping sprites. Here I would have multiple Sprite[] arrays like

    IdleSprites[]
    WalkLeftSprites[]
    WalkRightSprites[]

    and then in my Update() method I do


    if (idle) {
    GetComponent<SpriteRenderer>.sprite = IdleSprites[some_counter_variable];
    }


    The question for option 3) is if Unity eats up too much VRAM for this because when I have 200 enemies on the screen and all of their instances have multiple Sprite[] arrays, does Unity cache/share this somehow?

    Is using the Animator with pre-saved animations somehow shared for all 200 enemies I have on the screen even if they are in different states (walk idle jump attack dying)

    Option 3) would be very nice because I could have 1 code for almost everything I do. I could have code like (EXAMPLE ONLY)


    public string enemyType = "knight";
    void Start() {
    this.IdleSprites[] = Resources.Load("assets/sprites/" + enemyType + "/idle/*.png"
    this.JumpSprites[] = Resources.Load("assets/sprites/" + enemyType + "/jump/*.png"
    }


    next question is if this would slow down the game if I spawn 200 enemies or is this also pre-cached if this is a prefab.

    I could re-use all of my code this way. If I want a new enemy like "demon" I just add this same script to the prefab, change the "knight" variable to "demon" and everything should work right out of the box without creating new animator, new animations, new states etc
     
    Last edited: Mar 29, 2023
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    If you're worried about performance, test it. Spawn 200 enemies and see if it slows down the game; no one will be able to give you a more accurate answer than if you just test it yourself.

    Never use transitions with sprites, it's a complete waste of effort. Messing around with transitions is only ever useful when you want to smooth different animations together in bone based animation.

    This is the most sensible option in my opinion. It's simple and effective. However, if you were unaware, you'll want to get a reference to the Animator instead of calling GetComponent every time. Example:
    Code (CSharp):
    1.     Animator anim;
    2.  
    3.     bool walking = false;
    4.  
    5.     void Awake()
    6.     {
    7.         anim = GetComponent<Animator>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         if (walking == true)
    13.         {
    14.             if (anim.GetCurrentAnimatorStateInfo(0).IsName("Walking") == false)
    15.             {
    16.                 anim.Play("Walking");
    17.             }
    18.         }
    19.     }
    If this is your preferred option then you should go for it, assuming the performance is okay.