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. Dismiss Notice

Make my script play random animations

Discussion in 'Scripting' started by cruising, Jun 26, 2014.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello.

    I have a scrip "JS" here that makes "Fire1" "Fire2" play attack animations, but i want to use Fire2 for other things.
    So i need Fire1 to randomly play different attack animations when clicking the button, but how?

    Here is the code that works just fine how it is atm.
    Code (JavaScript):
    1. #pragma strict
    2. var charController:CharacterController ;
    3.  
    4. private var isAttacking : boolean ;
    5. function Start (){
    6.  
    7. animation.wrapMode = WrapMode.Loop;
    8. animation["attack2"].wrapMode = WrapMode.Once;
    9. animation["attack2"].layer = 1;
    10.  
    11. animation["attack1"].wrapMode = WrapMode.Once;
    12. animation["attack1"].layer = 2;
    13. animation.Stop();
    14. }
    15. function Update(){
    16. if(Input.GetButtonDown("Fire1")){
    17.     isAttacking = true ;
    18.     slash() ;
    19.     isAttacking = false ;
    20.     }
    21.    
    22. if(Input.GetButtonDown("Fire2")){
    23.     isAttacking = true ;
    24.     slash2() ;
    25.     isAttacking = false ;
    26.     }
    27. }
    28. function slash(){
    29. animation.CrossFade("attack2");
    30. Debug.Log("isAttacking1 is" + isAttacking);
    31. }
    32.  
    33. function slash2(){
    34. animation.CrossFade("attack1");
    35. Debug.Log("isAttacking2 is" + isAttacking);
    36. }
     
  2. katsuragi545

    katsuragi545

    Joined:
    Jun 25, 2014
    Posts:
    38
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Thanks for your reply!
    But of some reason only the first animation plays, idk if it is the code or if it is me who place it in the wrong way?

    Take a look (shows no errors)
    Code (JavaScript):
    1. function Update(){
    2. var rand = Random.Range(0,1); //this will return a 0 or 1
    3. if(Input.GetButtonDown("Fire1"))
    4. if(rand == 0)
    5.     {
    6.      slash();
    7.     }
    8.     else
    9.     {
    10.      slash2();
    11.     }
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    I could be wrong but I think Random.Range returns max value -1 when used with ints.

    Have you tried
    Code (JavaScript):
    1. var rand = Random.Range(0,2)
    to see if that works as you want?
     
    Last edited: Jun 27, 2014
    cruising likes this.
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Yes that helped. Thanks!
    But i think the animations for my character is to slow for a script like this, if i press 2 times when the first animation plays, i see half way in the first animation that the second animation tries to takes over so it just look weird. and if i press 1 time each second, i only see the first animation.

    maybe i need to use "queue" or some sort of a timer?

    EDIT:

    Now i have done it in this way, but i need something so that each animations plays 100% before the next one plays, so they dont brake other animations while playing.

    Code (JavaScript):
    1. function Update(){
    2.  
    3. var randomNumber:int = Random.Range(0, 4);
    4. if(Input.GetButtonDown("Fire1"))
    5. switch(randomNumber)
    6. {
    7.     case 0:
    8.     isAttacking = true ;
    9.     slash1() ;
    10.     isAttacking = false ;
    11.     break;
    12.    
    13.     case 1:
    14.     isAttacking = true ;
    15.     slash2() ;
    16.     isAttacking = false ;
    17.     break;
    18.    
    19.     case 2:
    20.     isAttacking = true ;
    21.     slash3() ;
    22.     isAttacking = false ;
    23.     break;
    24.    
    25.     /**case 3:
    26.     isAttacking = true ;
    27.     slash4() ;
    28.     isAttacking = false ;
    29.     break;**/
    30.    
    31.     case 3:
    32.     isAttacking = true ;
    33.     slash3() ;
    34.     isAttacking = false ;
    35.     break;  
    36.            
    37.  
    38. }  
    39. if(Input.GetKeyDown(KeyCode.LeftControl))
    40.     animation.CrossFade("taunt");
    41.     }
    42. function slash1(){
    43. animation.CrossFade("attack2");
    44. Debug.Log("isAttacking1 is" + isAttacking);
    45. }
    46.  
    47. function slash2(){
    48. animation.CrossFade("attack1");
    49. Debug.Log("isAttacking2 is" + isAttacking);
    50. }
    51.  
    52. function slash3(){
    53. animation.CrossFade("attack3");
    54. Debug.Log("isAttacking1 is" + isAttacking);
    55. }
    56.  
    57. /**function slash4(){
    58. animation.CrossFade("attack4");
    59. Debug.Log("isAttacking2 is" + isAttacking);
    60. }**/
    61.  
    62. function slash5(){
    63. animation.CrossFade("attack5");
    64. Debug.Log("isAttacking2 is" + isAttacking);
    65. }
    66.  
     
    Last edited: Jun 27, 2014