Search Unity

Different sprites with the same animation

Discussion in 'Animation' started by Morseton, Jun 13, 2016.

  1. Morseton

    Morseton

    Joined:
    Jan 15, 2016
    Posts:
    90
    Hello, is it possible to have the same animator for different sprite sheets?

    Let's say I have a top down character that can have different sprites.

    One of the spritesheets can have a leather cloak, another can have a sci-fi tshirt for example.

    Is it possible to have both run with the same animator to have different animations like:

    Aiming down the sights
    Punch
    etc...

    without having to add a different animation for each of the spritesheets?

    Thanks for your time!
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Yes - it is possible.
    What have you done to test this?
    Have you searched the forum for a solution to your question?
     
  3. RiskyBiscuitGames

    RiskyBiscuitGames

    Joined:
    Sep 2, 2013
    Posts:
    2
    The question was posted a while ago but since I came accross this issue and found my own solution I thought I'd post it here for any one who needs it.

    Below is a pretty simple system that will allow you to reuse the same animation sets with different sprite sheets. What you do is drag and drop all the sprite of the sprite sheet in the same order into the sprites array. Then while animating use animation events and call SetSprite using the index of the sprite in the array.

    Some notes on this is, from testing it seems to only work in play mode. Also if you have a single frame animation you need make the animation event happen after frame 0 of the animation as it might skip that one for some reason. Other than that it's a pretty simple solution that should save quite you a bit of work.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. [RequireComponent (typeof(SpriteRenderer))]
    5. public class SpriteSheetAnimator : MonoBehaviour
    6. {
    7.     public Sprite[] sprites;
    8.     private SpriteRenderer _spriteRenderer;
    9.  
    10.     public SpriteRenderer spriteRenderer {
    11.         get {
    12.             if (_spriteRenderer == null) {
    13.                 _spriteRenderer = GetComponent<SpriteRenderer> ();
    14.             }
    15.             return _spriteRenderer;
    16.         }
    17.     }
    18.  
    19.     public void SetSprite (int index)
    20.     {
    21.         Debug.Log ("Call");
    22.         if (index < 0 || index >= sprites.Length)
    23.             return;
    24.  
    25.         spriteRenderer.sprite = sprites [index];
    26.     }
    27. }
     
  4. Chambers88

    Chambers88

    Joined:
    Feb 25, 2018
    Posts:
    6
    I've been struggling with this problem for a year or so. The Animator unfortunately was not made to deal with sprite characters with multiple skins, which is something very common when making sprite-based games :(

    In one project, we solved this through having a spritesheet that contained the character in all poses on the left side and then a copy of the same poses, but with another skin, on the right side. We used the animator to animate the character with a default skin and a shader that would offset the pixels in the sprite renderer to determine what skin should be used.

    In another project, due to how the spritesheet was being made, we tried another solution, that was closer to Pendatic's. We are using the animator to "animate" a public int that determines which frame to render, from a list. Everything was fine until we started using the Timeline to make cutscenes. Animating an int instead of a sprite lead to all kinds of problems with interpolation. For example, when sequencing two animations with the Timeline, for a very quick time between these animations, the int slides from the final value in the first animation to the first value in the following animation...
     
    s4shrish likes this.
  5. s4shrish

    s4shrish

    Joined:
    Jul 6, 2017
    Posts:
    8
    Never thought I would see the same problem I had!!

    Thought I had made a really ingenious system where I had a car with different attachments but same basic animation cycle that needed to be seamless, by using an int that is animated, and based on the different bools the car with attachment sprites are used instead. A really good system, but as you said, there were issues. In my case it skipped a lotta frames, and causing some objects that were manually placed on it in Unity to go out of sync all the time. In the end, I had to make all of the sprites seperate GameObjects that are animated using same animation attached to the parent object. Had to do the same thing for each attachment in the animation window tho. :(