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.
2 Answers
2Pre-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.
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.
– TonyLiHey 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);"
– Pthom98
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
– nasa8Unity 4.3 introduced Animator.Play() [not Animation.Play()]. Say you have Mecanim 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);
– TonyLi