Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    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:
    6
    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:
    5,830
    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:
    6
    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:
    5,830
    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:
    6
    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:
    5,830
    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:
    6
    Thanks Joe!