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

[SOLVED] Animate my 2D sprite (frame loop)

Discussion in 'Getting Started' started by Nishinian, Mar 19, 2017.

  1. Nishinian

    Nishinian

    Joined:
    Mar 19, 2017
    Posts:
    5
    Okay, this one should be probably easy, but seeing as I don't know S*** and I can't find the solution, I turned to the forum.

    Problem: my sprite won't do it's animation-loop when playtesting my game

    Goal: I just want my sprite to play its 3-frame loop, even when doing nothing (idle loop), as soon as I hit 'play'.

    Version: Unity 4.6.9f1

    I searched for code but it always results in errors, I tried inserting a .gif asset, but obviously Unity doesn't support .gifs, and now I seperated my .gif into a spritesheet of 3 sprites (frames), and sliced it. So I got that far, now I only need to somehow tell the gameobject to start infinitely cycling through the sprites as soon as I hit play.

    For reference what I want to achieve (the moving spaceship & alien):
    https://www.instagram.com/red_pxl/

    Internet wizards, bestow upon me your wisdom.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well first, we have to ask, why are you using such an old version of Unity? I'd recommend downloading the latest unless there's some strong reason not to.

    But anyway, there are various ways to do 2D animation in Unity. In fact I wrote an article about it: 2D Animation Methods in Unity. If you still have questions after reading that, don't hesitate to ask!
     
  3. Nishinian

    Nishinian

    Joined:
    Mar 19, 2017
    Posts:
    5
    I'm using an old version of Unity because I'm following an online course in learning to code by making games in Unity. They use the older version of Unity. It upgrades in later classes to the recent Unity, but for now (and because it's just simple coding for now) I guess it suffices.

    Hm. I'm trying your first method, because this problem seems simple, I guess the simplest solution fits.

    My console is giving me this error:

    https://www.dropbox.com/s/3ckcwxgzjdln57i/List problem.jpg?dl=0

    Do I need an EventSystem or something?

    I tried adding <string> behind both the words 'List' in line 25, and the console stopped giving me this error but gave me four new ones :')

    Or do I need to replace some of your coding with my own code/words (strings) for instance?

    (Side note: 100+ lines of code, just for looping my sprite? I know that it's long because of all the extra options you built in into the Inspector but still, isn't that excessively long code for my problem?)

    Doesn't take away that I'm grateful for your response/help!
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You were on the right track; the error there is because you're using type "List" which doesn't exist in the namespaces you're using (defined at the top). This is my fault — I and/or GamaSutra failed to properly HTML-format that code, which is supposed to say List<Anim> rather than just List. I'm very sorry for the confusion.

    Fortunately you can download the demo project, including code for all three approaches, at the link given at the bottom of the article (also here for your convenience).

    Well, as you say, it's not 100+ lines just for looping your sprite. It's for setting up any number of animations, each with its own frames and speed, and letting you easily play, pause, and resume any of them.

    Sure, you could hack out a script that just loops through a fixed array of sprites at a fixed rate in probably 20 lines, but what would be the point?

    Or you can do it with no lines of code at all, using the built-in Animator features of unity... but as soon as you want to do something more than just play the same frames over and over, you're going to need some code like approach 2 or 3 in the article anyway.

    EDIT: if you're using an old version of Unity because you're following a tutorial that does so... well, doesn't that tutorial show you how to animate your sprite?
     
  5. Nishinian

    Nishinian

    Joined:
    Mar 19, 2017
    Posts:
    5
    Wow, I'm an idiot. So yeah, you were right. A few games further in the class they show how to animate sprites.
    It's with the built-in Unity method though.. And I'm the kind of person who wants to learn everything the right way the first time. I'm expecting they will dodge all the tricky stuff in the toturials, which leaves me with an animated sprite at the end, but not much knowlegde as how to do it right in coding..

    What is the best way in your opinion? Full-out coding, half-half, or full-out depending on Unity's animation features with as little coding as possible?
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, I tried to go into the pros and cons of those approaches in my article, so there's not much point in repeating it here. The only short answer is "it depends."

    My advice is to go ahead and follow the tutorial as they have it. It's a valid approach and necessary in some circumstances (for example, when you need to animate colliders to track the change in sprite shape). And you'll also come away with a good feel for when that approach is troublesome, which may or may not inspire you to try a more code-driven approach next time.
     
    Nishinian likes this.
  7. Nishinian

    Nishinian

    Joined:
    Mar 19, 2017
    Posts:
    5
    Thanks Joe!