- Home /
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.
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[go];
}
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.
The sprite sheets for ork is "ork" and for the skeleton is "skelleton". Each single sprite within the sheet is numbered 0 to 6 (on both)
Forgive me if I don't understand this very well, I am currently down with the Flu virus.
Why don't you just have two separate prefabs, an "Ork" and a "Skeleton", each with their own animator & animations?
Then based on the random number, Instantiate either the Ork or the Skeleton prefab?
Since this is a 2D animation and thus requires the sprites to change for the animation to change, I am confused as to what you mean by "one animation with different sets of sprites". For animations in Unity, as long as something isn't changed in the animation itself, it will always use whatever is currently set. So as long as the animation doesn't change the sprite, then your animation won't effect your other code that changes the sprite.
The Ork and Skeleton was just a simple example to figure out if I can exchange the sprites of an animation. What I really want to to achieve is the following:
I have for example 2 characters, one with pale skin, one with darker skin. I created for each of them a sprite set vor a moving animation: $$anonymous$$ale_1_$$anonymous$$oveLeft1, $$anonymous$$ale_1_$$anonymous$$oveLeft2, $$anonymous$$ale_1_$$anonymous$$oveLeft3 and $$anonymous$$ale_2_$$anonymous$$oveLeft1, $$anonymous$$ale_2_$$anonymous$$oveLeft2, $$anonymous$$ale_2_$$anonymous$$oveLeft3
Then I have two types of pants each also have a set of sprites Shirt_Long_$$anonymous$$oveLeft1, Shirt_Long_$$anonymous$$oveLeft2, Shirt_Long_$$anonymous$$oveLeft3 and Shirt_Short_$$anonymous$$oveLeft1, Shirt_Short_$$anonymous$$oveLeft2, Shirt_Short_$$anonymous$$oveLeft3. Same goes for pants, hair etc.
Now I want to have only one animation "$$anonymous$$oveLeft" which I can re-use for both characters and both types, to randomize my characters. Let's say $$anonymous$$ale1 with Shirt_Long or $$anonymous$$ale 1 with Shirt_Short etc. Since the animation is just a sequence of sprites, I thought changing the sprites within the animation would be sufficient but I can't figure out how.
$$anonymous$$y approach right now is to have one AnimatorController. Each character has several GameObjects (One for each part (Body, shirt, pants etc) with an AnimatorController attached to each. For each character/shirt/pants I drag and drop the respective sprites into the scene and create an animation. Then I create an AnimatorOverrideController, assign the animations to them and place them into the resources folder. In a script I load all the AnimatorOverrideControllers and randomly assign them to the AnimatorController in the GameObjects for each part.
This works but it will need a lot of AnimatorOverrideControllers and animations and I thought there might be a better solution.
Thanks for your help. I hope you'll get better soon.
Answer by CarterG81 · Feb 11, 2017 at 05:38 AM
Does this link answer your question?
http://answers.unity3d.com/questions/1275302/how-to-manage-2d-animation-states-of-same-behaviou.html
I read this and I think making my own animation system seems the way to go. Thanks for your help.