Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
5
Question by dakshesh1010 · Feb 27, 2017 at 06:33 AM · animationanimatorruntimeanimator controllermechanim

How to change animation clips of an animator state at runtime? Is there a way?

So I have 11 unique characters which are playable, as well as can be bots in VS CPU mode! I have 2 characters while others are still in production. I'm using mechanim for animation transitions. I want all of them to use a single animator.

Can I change the animation clips in an animator state at runtime? Because I still need to create animation clips for each character individually (which is fine), but I don't want to create animator for each of them again! I want to use a sinlgle animator!

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

5 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by WoofyWyzpets · May 01, 2017 at 02:21 PM

You can change animation clips in animators at runtime using a class called RuntimeAnimatorController.

How I used it: I have GO with an animator, it only has 1 animation but I want to swap the clip at runtime. (I have lots of very similar animations in structure and timing, just like different sprites, and I don't feel like making dozens of extremely similar animators just because the sprites change)

This script changes ALL animations in the animator into the animation clip specified in the inspector. You can change multiple animations individually by checking their name in the foreach loop or something.

 // Animator animator (get component blablabla)
 // AnimationClip anim (field in inspector)
 
 if(anim)
 {
             AnimatorOverrideController aoc = new AnimatorOverrideController(animator.runtimeAnimatorController);
             var anims = new List<KeyValuePair<AnimationClip, AnimationClip>>();
             foreach (var a in aoc.animationClips)
                 anims.Add(new KeyValuePair<AnimationClip, AnimationClip>(a, anim));
             aoc.ApplyOverrides(anims);
             animator.runtimeAnimatorController = aoc;
 }

Original idea thread, apparently there are no other way to change clip yet: https://forum.unity3d.com/threads/change-animation-controller-state-motion-clip-at-runtime-in-unity-5.345142/#post-2291758

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 losingisfun · Mar 22, 2018 at 04:21 AM 0
Share

this is the best answer. You should also check the docs here, there's another example, and explains it some more: https://docs.unity3d.com/ScriptReference/AnimatorOverrideController.html

avatar image
1

Answer by idbrii · Jan 30, 2018 at 12:24 AM

Instead of specifying override clips in code, you can use the AnimatorOverrideController asset.

The AnimatorOverrideController Manual page describes how to setup an AnimatorOverrideController asset.

You can swap to it in code with:

 public AnimatorOverrideController CharacterAnimator;

 // Call this function from somewhere.
 void SwapToOverride()
 {
     GetComponent<Animator>().runtimeAnimatorController = CharacterAnimator;
 }

I believe this is the first method AnimatorOverrideController ScriptReference refers to when it says:

1 . Create an Animator Override Controller in the Editor.

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

Answer by Adeel1 · Feb 27, 2017 at 09:23 AM

@dakshehs1010 Yes it is possible if you are using animator or animation component for your character. Put multiple animations in ANIMATOR window. You need to set the transition states from one animation to other animation in animator window including the default state. Set the transition conditions that could be of bool, int or string type. Use those transition conditions in your script to change the animations on runtime. e.g Public Animator anim; anim.setBool("YourAnimationName", true/false);

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

Answer by dakshesh1010 · Feb 27, 2017 at 09:59 AM

Thanks @Adeel1 for responding, yes I've already done that. The problem is something else. My bad I guess. I couldn't explain my problem more elaborately.

The problem is, I want to change the clips stored in the states of the animator at run-time so that I don't have to make new animators for each and every character. I want to use just one animator across all characters, which is not possible unless the animation clip in all the states of animator is changed every time the character is changed!

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

Answer by AlharbiSunds · Jan 10 at 01:06 PM

I know its old question, but I was looking for the same. I found this tutorial, It seems will work https://www.youtube.com/watch?v=VHYke1HrlMY

https://www.youtube.com/watch?v=wip2kzaF728

https://docs.unity3d.com/ScriptReference/AnimatorOverrideController.ApplyOverrides.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

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

151 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 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 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 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

Can animation clips be swapped out of the animator override controller in runtime? 1 Answer

Get weird issue when change from Mechanim -> Ragdoll 0 Answers

Why do my animations only play once? 1 Answer

Animation Running Game 0 Answers

Building Tilemap Animations in code require AnimationController 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