Create random sprite characters with animations

Hi there,

I looked a while around but couldn’t find a good answer for my problem. What I want to do is the following:

  • I use sprite sheets and animations where a couple of sprites are shown one after another (No bones or something like that. Just plain old sprites played in a sequence
  • I have several parts like hair, pants, shirt etc which are also can be played sequentially to get an animation
  • Now I want to use my animations for the pants and swap out the sprites to another set of sprites (e.g. Animation ‘move left’ now has long pants and I want to apply sprites of short pants for the same animation
  • I already tried swapping out the sprites in LateUpdate() and it works, but I actually want to click a button and generate a completely randomized character which can use all the animations I’ve done before

That is what I have already tried. For testing I use two sprite sheets. One is an Ork another one is a skeleton with a simple movement. As you can see, the animation is quite the same.
The Prefab in the script contains the animation of the ork and by choosing randomly I want to create a skeleton.

87593-ork.png

87594-skelleton.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NPCGenerator : MonoBehaviour {

    // Prefab already has an Animator
    public GameObject Prefab;


    private Dictionary<GameObject, Sprite> ToChange = new Dictionary<GameObject, Sprite>();


    public void GenerateNPC() {

        int random = (int)Random.Range(0, 2);

        Sprite sprite = null;

        if (random == 0) {
            var sprites = Resources.LoadAll<Sprite>("Sprites/" + "ork");
            sprite = sprites[0];
        }

        if (random == 1) {
            var sprites = Resources.LoadAll<Sprite>("Sprites/" + "skelleton");
            sprite = sprites[0];
        }

        ToChange.Add(Instantiate(Prefab, new Vector3(13, -7, 0), Quaternion.identity), sprite);
    }

    void LateUpdate() {
        foreach (var go in ToChange.Keys) {
            go.GetComponent<SpriteRenderer>().sprite = ToChange
;
            }
    
            ToChange.Clear();
        }
    }
    

If I start the game and click the button, I sometimes can see for a very sort moment the skeleton but than it is changed to the ork.

Is there any way how I can use one animation with different sets of sprites?
  
Thanks for your help.

  
  

Does this link answer your question?