Search Unity

Animating a customizable 2D Sprite Sheet character

Discussion in '2D' started by lennartkueppers, Feb 11, 2020.

  1. lennartkueppers

    lennartkueppers

    Joined:
    May 23, 2018
    Posts:
    20
    Hello,

    this is a very common problem during the development of a 2d pixel-rpg-style game.
    I have a fully animated character, animated through a spritesheet and of course unities animation controller.
    But I want this character to be customizable, so hat, clothes, shoes, weapon etc.
    My idea was to create "overlay spritesheets" just containing the animated weapon or clothes part, and adding that to the character.
    So basically to divide the character into seperate parts.
    But how can you actually swap these sheets and still keep the animation in the animation controller? I mean the animator (the animation) is referencing the specific sprites in the asset folder. How could you swap these in a simple way? Also I saw an answer, where someone said that the new spritesheets sliced parts must have the same names to exchange different parts properly.
    I dont think this is the best solution. I have also implemented an editor script that automatically does that, but still I cant imagine that most game developers do that.
    There must be an good and easy way to achieve this or not?

    My expectation (when this problem will be solved) is that later I can just draw more and more spritesheets (stuff), put that into my Asset Folder, and just drag and drop this into an array, or call the name of the sheet or something similar.

    There are tons of other people who asked this question, but I never really saw a completely satisfying answer or a helpfull video.
    Concrete examples, ideas or material would be great.

    Can you please help me with this problem?
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    It might be easier to use keyframe animation. Instead of animating through spritesheets, you could animate (primarily) through moving the Transforms of GameObjects. Tutorial.

    If you keep each character's hierarchy organized the same, you can reuse animations over multiple characters with different hat, shoes, weapon, etc so long as their body sizes are relatively similar. Because AnimationClips/Controlls will use the name of the body parts, you can use the same animation clips for characters with different sprites for the Body, Shoes, Weapon etc. Customizing the character should then be as simple as swapping the sprite because the animation is not done through a spritesheet: movements are done through mainpulating the position/rotation of the transform with Curves. So long as that transform has the right name, it should work.

    Example organization of Hierarchy:
    Code (csharp):
    1.  
    2. Character (top-level GameObject)
    3.    - Body
    4.       - Foot    (can have any shoe sprite)
    5.       - Foot    (can have any shoe sprite)
    6.       - Arm
    7.            - Weapon   (can have any weapon)
    8.       - Arm
    9.       - Head
    10.           - Hat
    An "attack" animation would need to be custom per-weapon or weapon-type because "fire pistol" and "swing sword" movements will be different.
     
    lennartkueppers likes this.
  3. lennartkueppers

    lennartkueppers

    Joined:
    May 23, 2018
    Posts:
    20
    Thank you I will try that out and let you know how it worked for me.
     
  4. lennartkueppers

    lennartkueppers

    Joined:
    May 23, 2018
    Posts:
    20
    But actually I dont think that would be the solution...I mean my character is completely self drawn and I dont think that manipulating the different transforms will work cause all the different positions/slices of my char (lets say 30) are different
     
  5. Durium

    Durium

    Joined:
    Feb 1, 2020
    Posts:
    37
    I am doing the same project ish here and what i think the guy above you meant is when you make the spritesheet you need to seperate it into Head, Body, Arm, Legs, Feet
    Instead of just having one sprite as a character.

    Then in the unity animation recorder you allign the bodyparts correctly instead.

    That is what i think he meant, although i havent done it myself yet so ^^
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    have a script that detects which animation and frame is currently playing

    animation 3 frame 5 ( for example )

    then have an array of arrays with sprites for each animation

    then sprite = spritearray[3][5] , in this example

    when you want to change a customized part just change the sprites on the sprite array
     
    DeanTheodorakis likes this.