Change animation sprite depending on condition

Hi all,

I am facing an challenge for more or less a week, looked for more answers and similar questions, youtube videos and unity tutorials but none looks to solve this specific case. I would really appreciate any help with this:

Let’s say I have a 2D character that can move in four directions. This character has the hability to pick up food. Food can be eaten and has different sprites depending on the remaining food.

Right now, my Animator looks like this:

  • Idle
  • Walking
  • Picking

And for each of these states I have added the direction, so:

  • Idle

  • Right

  • Left

  • Front

  • Back

  • Walking

  • Right

Now, depending on the remaining food I also have a condition for the animation that has to be triggered:

  • Idle

  • Right

  • 100%

  • 75%

  • 50%

  • 25%

  • 0%

  • Left

  • 100%

So I end up having animations like PlayerIdleLookingRightWith100Food, PlayerIdleLookingRightWith80Food …

Obviously, the quantity of animations is growing exponentially whenever I add a condition. I have created over 200 sprites just for handling all these animations for a single character. If I added another condition like changing the skin tone depending on the amount of food, I would need 1000 sprites and more or less 400 different animations.

I guess I am missing something, but for me it is estrange that none has this same problem so I am a little bit lost. Could someone please help me?

This is the rabbit hole of customization, it is more an art problem than a code problem, the more variable your character the more exponentially your possible combinations will grow and every way is messy.

You will need to separate the body parts and equipment of your character by layer and create your sprite by combining all different parts.

for example: 1 layer for skin, 1 layer for clothes ( 2 sprite renderers)

The more different layers you need the more sprite renderers.

There is another way of combining layers using texture2d.setpixels, this combines every layer into 1 sprite using code but there is more coding work and needs more processing power.

For very customizable characters you can expect to have over 1million .pngs ( keep them in the resources folder though, the trick is to load only the parts that you need for each specific character combination that is on screen, and unload what isn’t needed )

Thanks for your reply, brigas.

So, let me see if I understood correctly. I should generate sprites for each of the layers (head, body, legs, equipment). I guess this layers must be pixel perfect, meaning that if the head of size 10x10 for walking animation must be at position 10,15 in a 64x64, I need to generate the sprite 64x64 and cannot generate it 10x10 and then reposition it.

That results in having like 800 64x64 sprites that will have an average of 80% of their pixels transparent. And more or less 200 animations for each player. It’s quite complex but completely doable.

I guess the challenge here is to find the balance between the exponential increase of the sprites and the code/animations complexity.

correct, the best way is like having layers in photoshop, each part of your character in one layer and then you hide all layers except one and save, and do this for each layer.

If you really want to save on the transparent space you can crop it but you will have to do some math to calculate the sprite pivot point for each body part and set it on each sprite.

As I said the main trick is that you will never need every single sprite loaded at the same time so it will mostly just take disk space( and not that much, each png should be 3kb ( if you have just 800 thats 2,4mb )

1 Like