Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 28, 2014 at 12:23 PM by nasa8 for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by nasa8 · Dec 04, 2013 at 06:13 PM · animationrandom

Playing random animation by Animator Controller

Hello! Is it possible to play one of a few animations for an animation state? For example I have animation state "Attacking" and I want to play three different attack animations randomly.

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

  • Sort: 
avatar image
3
Best Answer

Answer by TonyLi · Dec 04, 2013 at 06:46 PM

Pre-4.3, we would create an Integer parameter -- for example, called AttackVariation -- and create transitions to the three attack animations. The transition to attack #1 would have a condition "AttackVariation Equals 1", the transition to attack #2 would have a condition "AttackVariation Equals 2", etc. Then, to trigger a random attack in script, set the AttackVariation parameter to a random number [1,3].

In 4.3, you can Play() or CrossFade() directly to states. Put your attack state hash IDs in an array, and randomly CrossFade() to one of them.

Comment
Add comment · Show 3 · 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 nasa8 · Dec 05, 2013 at 06:34 AM 0
Share

Initially I tried to use some code with Animatio.Play(...), but transitions between states in code became too complex so I decided to use Animator as a more convenient tool. The way with a few transitions for random animations is clear but not very efficient. Do you mean using code as I used before or something else?

p.s. using unity 4.3

avatar image TonyLi · Dec 05, 2013 at 01:50 PM 1
Share

Unity 4.3 introduced Animator.Play() [not Animation.Play()].

Say you have $$anonymous$$ecanim Animator Controller states named "Attack 1", "Attack 2", and "Attack 3". Use this:

 string attackState = string.Format("Attack {0}", Random.Range((int) 1, (int) 4));
 animator.Play(attackState);
avatar image Forceman · May 02, 2014 at 02:18 PM 1
Share

Hi, I need advice. We try to make my player had every time you start or restart the game had a different animation. I have set the Animator animation as a state and when you add more animation to a new state, so I do not know exactly how to write code or how to properly set the animator. try it for about two weeks and I still can not. I'll be glad for any advice or help with code and setting animators. I'm concerned about your progress with attackvariation that you describe. I create tnasition of Any State to my animations? or both? and the code did not help, I'm a newbie and I manage it the basics as most of the code can handle, but here I'm not very good. thank you again ... otherwise my player has the Inspector assigned animator and animation animators do I, but if I want to change in a more random, so I end:-D

avatar image
0

Answer by Grohowiak · Dec 04, 2013 at 06:50 PM

Check this thread. Random animations for idle state but might fit what you are looking for.

Unity Forums

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 TonyLi · Dec 04, 2013 at 06:59 PM 0
Share

If you're willing to go to Unity 4.3, I recommend using Play() or CrossFade(). That thread was back in 2012 when we didn't have API access to set states.

avatar image Pthom98 · Dec 11, 2016 at 09:23 AM 0
Share

Hey i need some help Im new to the coding part of unity and trying to make a medieval combat system so when i press middle mouse i get a variety of two different attacks being left or right randomly and i know this like of code in the best answer will work but ive got a compiler error for the like "(attackState);"

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to play transitioned animation at random time 1 Answer

Handling Animation and movement speed 0 Answers

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Random attack animations in java. 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