Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Syrious · Feb 09, 2017 at 03:37 PM · animation2dsprites

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.

alt text

alt text

 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.

skelleton.png (2.4 kB)
ork.png (2.4 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Syrious · Feb 07, 2017 at 09:34 PM 0
Share

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)

avatar image CarterG81 · Feb 09, 2017 at 08:06 PM 0
Share

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.

avatar image Syrious CarterG81 · Feb 09, 2017 at 09:32 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Syrious · Feb 11, 2017 at 02:20 PM 0
Share

I read this and I think making my own animation system seems the way to go. Thanks for your help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animating 2D Character made up of 2 or more swapable sprites 1 Answer

I have an animaiton problem about sprite loading. 0 Answers

Is Unity capable of implementing Complex 2D Characters/Animation/Sprites 1 Answer

Alpha masking multiple 2D Sprite GameObjects with 1 Sprite GameObject 0 Answers

Fast Sprites Extraction from Sprite Sheet 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges