I am starting work on a top-down style game. I want my character to be able to equip different weapons and armor they find along the way. What is the best way to accomplish this?
Currently, I have my character drawn and animated in Asperite. The character itself is just one sprite in each frame. I have seen some implementations where there is a head, torso, arms, legs, etc. Is this the approach I need to take to support a dynamic equipment system?
If I have to split the sprites up by part, where do I reconnect them? Are they all a part of a different layer in Asperite? Do I still do the animations in Asperite, or would that now be done in Unity?
you need to save each layer as different part+frame
then you have 2 options:
a) 1 sprite renderer for each layer ( easier but cost more memory, if you have many layers and characters the game will lag )
b) texture2d.setpixels to combine every layer into 1 frame ( harder, but you can create only the combinations that are visible so you save tons of memory, and the game runs smoother because its only 1 frame per character)
For approach A: I would create one game object with a child Head, Torso, Legs, etc. game objects underneath that are positioned correctly to resemble the player? Do I animate each component separately with that approach? Any way to reuse the animations I currently have from Asperite?
For approach B: I read the Unity Docs to gain some understanding of that function, but I’m not 100% of how a real implementation looks. Would that run on Update each time a new animation is used? And then if I equip a helmet it will set the head pixels to the pixels of the helmet?
for A: yes you would make a game object with as many child as there are layers. You take your animations from asesprite, you hide all layers except the part you want to save and then save the frame, then in unity, for example torso_walk_right_frame_1 > torso_walk_right_frame_2, plays at the same time as hair_walk_right_frame_1 etc
this approach is very easy but be careful with too many child objects ( I had a character with 49 layers and with 10 characters onscreen the game was unplayable )
for B: you create a struct that has information of your character customization. Represent your diferent customization parts as numbers like hair_1, torso_1, and then your struct will be like this:
public struct CharCustom{
public byte hair;
public byte torso;
public byte boots;
}
then lets say you want to create the combination for hair 13 torso 4 boots 5 you do
new CharCustom(13,4,5);
then you use texture2d.getpixels to look up the pixels of boots_5, torso_4,and hair_13 and texture2d.setpixels to draw them all on the same sprite then you hold that sprite and use it for your animations( you have to do this for all frames you need )
its not used on update because you only need to create new sprites when you change equip, and there are many possibilities that you will never have to create if the player never equips that combination which saves you memory.
Thank you so much for a detailed approach. I’m excited to try this out tomorrow. I think I will start with the first approach and move on to approach B further down the line.
Hi raarc, so I’ve attempted to implement approach A and I appear to have gone wrong either somewhere in the art or animation and I was hoping you could help out.
The issue that I’m running into is that each sprite is running through their animation (in my case I have 8 frames for my Walk Down animation with a Head, Arms, Body and Legs child game object with accompanying sprite renderer. My Animator is on the parent game object.
you need to save the full frame.( in the head frames dont crop out the space where the body would be just because you hid it, there should be a lot of empty space in each part )
( for this its better to have individual .pngs rather than a sprite sheet, go back to the spritesheet once you make it work with individual frames )
then also make sure that you have the same pivot for every sprite
and that every child object position in unity is 0,0,0