Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Same animations for multiple sprite variations

Discussion in 'Animation' started by piggymorse, Nov 10, 2020.

  1. piggymorse

    piggymorse

    Joined:
    Jul 18, 2016
    Posts:
    10
    I'm fairly new to Unity and haven't been able to figure this one out yet. I've created an enemy which has various animations - idle, attack, injured etc. These animations were created in the Editor by dragging a bunch of sprites into the animation window and then spacing them out so that over the course of about half a second, 10 different sprites gets cycled through giving the illusion of an animation. All good so far...

    I now want to create a bunch of enemy variations that all use exactly the same animations but cycling through different sprites. I assumed there would be a very easy way to do this but unless I'm mistaken, the sprites become part of the animation file and can't be swapped out? This creates a bit of an admin nightmare because if I have 20 enemy variations with let's say 5 different animations, I now have to create 100 animations. Further more, if I later decide to change an animation or add a new one, I have to do it 20 times.

    I was hoping I could simply have a bunch of variables on the enemy class to hold the sprites and in code assign them to the animation somehow. Before I give up trying for an elegant solution and go ahead creating all those animations manually, I thought I would ask here. I really hope I'm missing something very simple here.
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,340
  3. piggymorse

    piggymorse

    Joined:
    Jul 18, 2016
    Posts:
    10
    As it happens, I figured out a way to do this about an hour after posting. Typical.

    For anyone interested, I used animation events. So where I had my animation cycling between 10 different sprites, I removed the sprites from the animation timeline and replaced them with 10 animation events at the same points.

    An animation event allows you to run a method whenever the animation reaches that event in the timeline, so I created a method in my enemy class to change the gameobject's sprite from a variable and then I'm just calling that in the animation events.

    Creating an enemy variation is now quick and easy, I just need to duplicate the enemy gameobject and set the variables to the new sprites and since I can set the variables in code, it also means I can change enemy variations at runtime as well if I want to.