2D spritesheet animation reuse and swapping

For a top down 2d game, say I have a character walking animation with 16 frames (4 frames each for up, down, left and right).

Usually, I would just make the 4 corresponding animation clips and configure an animation controller. However, if I have 20 other characters with the same see spritesheet format, how can I reuse the animation or swap out sprites on the go. Like imagine I have a character that is animated and at runtime I’d like to swap his spritesheet with another and retain the same animation format. How do I go about this?

Try having an int variable and have a “player” object linked with multiple children (being the characters), then edit the children by having them all be separate public GameObject variables, coded entirely by the parent. Turn on the sprite renderer and only code the child corresponding with the number the int is. This way, unless you want the characters to act differently, you will only really need 1 script and only on the player. Tell me if you need more information or have any questions!
Note: I actually have no idea if this will really work, but hey, it’s worth a shot right? I’m 99% this would work.

So how would this work exactly. The sprite renderer will only take a single sprite, but I need it to cycle through the various sprites using the animation. Also this wouldn’t scale if I had say 50 or so such spritesheets. Like imagine I have 50 spritesheets that would be 50 separated animated characters. I don’t want to make animation clips and controllers for each one. Hence I want to reuse the animation part. That’s what I’m aiming for

Then turn off the animator? Just turn off something or other so you can’t see the characters you aren’t playing as.

You could feasibly just have the amount of sprites you need all as different children in the characters, but I have a feeling that would take way too much work. Hate to break it to you, but if you want to create this kind of effect, this is the best way I can think of doing it. Less you have another idea. Any more questions?

The first way I know of is Animation Override Controllers. This involves of course making animations for each different character only you don’t need to redo the controller over and over again. I’ve done this, it works fine.

The second option is to use Sprite Library Assets. I have not used them yet, so I couldn’t go into detail. I looks promising, and should do what you want easier than the first way.

1 Like

To be honest I had no idea those were even things that existed.

have a script that checks which frame the animation is currently on and change to the correct sprite.

for example direction left, frame 3, change the sprite renderer to the correct sprite

you need to have the animator on a different object than the sprite renderer, because the animator will always try to ovverride the sprite renderer if you change the sprite

1 Like

This works! thanks for the idea!