- Home /
The question is answered, right answer was accepted
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.
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.
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
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);
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
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.
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.
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
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