0
\$\begingroup\$

I have been building a 2D sprite-based game for which I want to have the player be able to customize their equipment. This means that although I am fine with drawing the content, I'd need to ensure animations in the game run fine on top of each other. For this, I have been preparing a game object with several children to account for the equipment:

main camera here is just so the player is followed, so it shouldn't be taken into account

player body pants boots shirt tunic belt

Each of the children runs a single animation and should have to follow the player, which I accomplish by using transform.localPosition = Vector2.zero; on the Update of the script each takes, so they hook to the parent's pivot and follow the player. While this has worked for the most part, there are moments in which all of the objects are not synchronized and as such sometimes the parent object (the body) is seen where it shouldn't since the other game objects should render on top:

enter image description here

Aside from that, to make it easy for the children to follow the parent position I had made sprites which are all the same size, which risks me having to load a lot of transparent space per sprite.

Another problem that I just noticed as I'm trying to address the issue of loading too many useless pixels involves the positioning with other objects such as the Sword game object, which doesn't follow the player fully if I use sprites that are not perfect squares (see this question for details and this one).

I tried to fix this by making the Sword a child of the Hero, but even then changing the position through a function that sets values to add on to transform values of the sword game object only change the position of it relative to the initial value. I attempted changing the pivot of the sword sprites to a custom value to guess where the center of it would align with the main game object and appear in the right position, but even that doesn't seem to work.

I'm kind of getting tired with my current process, as I have to rely on several animations for each of the game objects, both parent and children, so that these obey to different layers in a single animator (or in the case of the sword, a separate animator), all to ensure there is some synchronization that doesn't always occur:

enter image description here enter image description here

I really don't mind the web that is turning out in what I'm doing, but the fact that I have to repeat it across multiple layers with no real guarantee that all the objects would appear right on top of each other due to the fact of having multiple animations playing, and loading multiple sprites with empty space is becoming more of a chore than enjoyment.

So I think I came up with a possible solution: If I could make a single animation for the whole equipment used at any given point (whether only wearing pants or wearing full equipment), then having this single animation could guarantee synchronization across parent and children without the need for animator layers or special functions to update position or worrying about pivots or square sprites if I can set the position of non-square sprites in the animation, with the downside that I would need to account for every single animation for each possible equipment variation (so if I had even 3 of each sword, pants, boots, etc. that would mean 3^6 animations) and make a more complex web of animator states. The only thing I'd be worried about in this case, however, would be the performance, if having too many animations for a player would affect how fast these load. But at the benefit of eliminating the other problems mentioned, my question boils down to this:

Is it better to have a single game object with animations that change multiple sprites across children game objects and a single animator that chooses states based on multiple variables, or game objects with multiple animations that change a single sprite for each, and a single or multiple animators with multiple layers that choose states based on multiple variables?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Looks like you should be more concerned with scalability than performance issues. Your state machine is scary \$\endgroup\$
    – Mangata
    May 1 at 10:22
  • \$\begingroup\$ I mean, I know I can nest like here docs.unity3d.com/Manual/NestedStateMachines.html but I haven’t tried yet. I always felt the way I was doing animations/sprites might not be the correct one, so I didn’t consider fixing the state machine until I fix the animations properly \$\endgroup\$
    – gfcf14
    May 1 at 11:22
  • \$\begingroup\$ If the sprites basically mimic the state of your character, why cant you give it the person animation controller instead of creating a new one for each of them? \$\endgroup\$
    – Zibelas
    May 1 at 11:48
  • \$\begingroup\$ @Zibelas the sprites should use the main game object’s animator, but in the cases where I needed to use an external game object to mimic the main one I needed a different animator. It shouldn’t matter anymore because one such case was when I had the sword not be a child of the main, but I put it back as a child \$\endgroup\$
    – gfcf14
    May 1 at 12:45

0

You must log in to answer this question.

Browse other questions tagged .