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 Omega2D · Sep 28, 2015 at 03:01 PM · c#unity 5spriteanimatorcharacter

How to change the sprite and the animator in script to make a character selection?

Hello everyone,

I need your help to know how to make a character selection in my game like the character selection of crossy road game.

My characters will use the same animator controler I only change the sprite and the animation when you change the character.

So please somebody know how to do this in script?

P.S:I using the unity 5.

Thanks for all.

Comment
Add comment
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

2 Replies

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

Answer by Tomer-Barkan · Sep 28, 2015 at 03:23 PM

Up to 5.1 you are not able to change animations in an animation controller during runtime. I think I read they might have added this capability in 5.2, but I'm not sure.

Anyway, the alternative, which might even be cleaner, is to use AnimationOverrideController. You can create these objects with right click in your project, and they are simple objects in which you select an original animation controller, and then for each animation of the original controller you provide a different animation (or leave empty to use the original).

This allows you to set an animator controller for selected objects while keeping all the state machine logic of the original controller. The replacement animation will have all the "selected" sprites. Then in runtime, all you have to do is switch one animation controller from the original to the selected version, instead of replacing all the animations from code.

Comment
Add comment · Show 2 · 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 Omega2D · Sep 28, 2015 at 04:10 PM 0
Share

Thanks $$anonymous$$er, I think this will work very well

avatar image Omega2D · Oct 26, 2015 at 03:25 PM 0
Share

Hey $$anonymous$$er is exactly i waht i need, but i have some trouble to implements in the script, please can you tell me how the animator override controler knows how to substitute example "run1" for "run2" and not "run1" for "jump2" i didn´t fount this in the documentation

avatar image
2

Answer by retry_entry · Oct 18, 2017 at 10:30 AM

There is a better solution. Use this script - https://github.com/RetryEntry/UnityAnimatorChangeSprite Please give thumbs up, cause I have spent two days searching through useless solutions like one that soulburner suggected. If you have 15 gameobjects with 15 sprites in animation chain you will spend up to 225 loops every frame searching for sprites to swap to... With such approach you CAN'T create for instance charactes with different clothes animated by one animator, cause you will spend 12000 loops every second on useless things like searching. With this script you will not spend time... It uses shaders. You can change it as you'l like, Just star please. I have spent two days... :/

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 robbel · Oct 20, 2017 at 01:23 AM 0
Share

Yo, this is nice :) i had to tinker with it a little bit to work but the solution works very well.

(i removed everything from the awake funktion as i dont need it and also added a public field shader so i dont need to place it into a resource folder.)

Well done!

edit: for those interested, here is my customised script (sorry i dont get the code to be displayed appropriately):

public class SwappedTextures : $$anonymous$$onoBehaviour {

 public Texture2D swappedTexture = null;

 public Shader swapShader;

 private SpriteRenderer renderer;

 private void Awake()
 {
     renderer = GetComponent<SpriteRenderer>();
 }

 public void SwapTexture(Texture2D texture)
 {
     swappedTexture = texture;
     $$anonymous$$aterial new$$anonymous$$aterial = new $$anonymous$$aterial(swapShader);
     renderer.material = new$$anonymous$$aterial;
     renderer.material.SetTexture("_$$anonymous$$ainTex2", swappedTexture);
 }

 public void Update()
 {
     if (swappedTexture != null)
     {
         SwapTexture(swappedTexture);
         swappedTexture = null;
     }
 }

}

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

39 People are following this question.

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

Related Questions

How would I switch between 2 animation states connected to 1 state under input? 0 Answers

Distribute terrain in zones 3 Answers

How can I change the sprite using an image URL? 1 Answer

Character face camera direction when this pass a rotation limit 0 Answers

Cant access the Image icon from class 1 Answer


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