Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to play random animation clip?

Discussion in 'Scripting' started by leegod, Aug 29, 2015.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,345
    I experimented with Animator and Animation component.

    So what I want is, if character now need to do [Attack] action, I want to play random one of 'Attacking' motions from many.

    I tried with Animation component and failed with many googling found few approach.

    How should I do?

    Thanks.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    C# :
    Code (CSharp):
    1. public Animation[] attacks; //Put attack animations here
    2.  
    3. void Start () {
    4.    Random.seed = System.DateTime.Now.Ticks; //Just for full randomization
    5. }
    6.  
    7. void Attack () {
    8.    Animation.Play(attacks[Random.Range(0,Mathf.RoundToInt(attacks.Length));
    9. }
    US:
    Code (CSharp):
    1. var attacks : Animation[]; //Put attack animations here
    2.  
    3. function Start () {
    4.    Random.seed = System.DateTime.Now.Ticks; //Just for full randomization
    5. }
    6.  
    7. function Attack () {
    8.    Animation.Play(attacks[Random.Range(0,Mathf.RoundToInt(attacks.Length));
    9. }
    I don't have time to test these codes, but if any error occur, please write a comment about it
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,345
    Thx for reply,

    But, what do you mean Animation.Play?

    Declare varible like,


    Code (CSharp):
    1. public Animation animate;
    2. public Animation[] animateClips;
    3. and,
    4.           animate = GetComponent<Animation>();
    5. and then,      
    6.           animate.Play(animateClips[Random.Range(0, Mathf.RoundToInt(animateClips.Length))]);
    then I got this error,

    Assets/Script/EnemyAnimControl.cs(52,17): error CS1502: The best overloaded method match for `UnityEngine.Animation.Play(UnityEngine.PlayMode)' has some invalid arguments
     
  4. 8bitCartridge

    8bitCartridge

    Joined:
    Feb 26, 2014
    Posts:
    144

    you need to declare an ANIMATOR from which you are playing your animation, something like this:

    public Animator _animator;
    public Animation[] animateClips;

    and then you play it:

    _animator.Play....blah blah blah

    basically, you play your aniMATIONS from your aniMATOR. Hope that helps
     
  5. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    Well now that's not entirely true since you can indeed also play aniMATIONS from your aniMATION component :)
     
  6. 8bitCartridge

    8bitCartridge

    Joined:
    Feb 26, 2014
    Posts:
    144
    @vintar
    anyWAY :D that's how I do it, just tried to help a fella... don't be so judgMENTAL :p
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Animation is legacy
    Animator is mecanim and more relevant (unless OP was using legacy)
     
  8. yousafggs

    yousafggs

    Joined:
    Feb 2, 2018
    Posts:
    1
    Code (CSharp):
    1. public Animation animate;
    2. public AnimationClip[] animateclips;
    3. and,
    4.           animate = GetComponent<Animation>();
    5. and then,    
    6.           animate.Play(animateclips[Random.Range(0, Mathf.RoundToInt(animateclips.Length))].name);
    because animation.play(String name) use string parameter
     
    Last edited: May 15, 2018
  9. pitibonom

    pitibonom

    Joined:
    Aug 17, 2010
    Posts:
    188
    this is old way of doing it. the legacy system ( wich IMHO was much easier to control through scripts than the animator system.... )

    any way of doing this with animator, transitions, states and all this obfuscated things ?