Search Unity

animation

Discussion in 'Scripting' started by 787gabriel777, Jul 27, 2015.

  1. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    Code (JavaScript):
    1. #pragma strict
    2. static var _animation:int;
    3. function Start () {
    4. }
    5. function Update () {
    6. switch(_animation){
    7. case(0):
    8. GetComponent.<Animation>().Play("idlee");
    9. break;
    10. case(1):
    11. GetComponent.<Animation>().Play("correr");
    12. break;
    13. case(2):
    14. GetComponent.<Animation>().Play("attackk");
    15. case(3):
    16. GetComponent.<Animation>().Play("waitingforbattle");
    17. break;
    18. case(4):
    19. GetComponent.<Animation>().Play("diee");
    20. break;
    21. }
    22. }
    23.  
    my animation has a problem
    if i start the animation correr
    the animation attack doesnt start
    but i need start the animation attack
    how i do that?
    Code (JavaScript):
    1. #pragma strict
    2. static var _animation:int;
    3. function Start () {
    4. }
    5. function Update () {
    6. switch(_animation){
    7. case(0):
    8. GetComponent.<Animation>().Play("idlee");
    9. break;
    10. case(1):
    11. GetComponent.<Animation>().Play("correr");
    12. break;
    13. case(2):
    14. GetComponent.<Animation>().Play("attackk");
    15. case(3):
    16. GetComponent.<Animation>().Play("waitingforbattle");
    17. break;
    18. case(4):
    19. GetComponent.<Animation>().Play("diee");
    20. break;
    21. }
    22. }
    23.  
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    You are starting the animations in Update(). Update() runs every frame. If you start an animation every frame then it'll always be at the start of the animation and look like it's not playing or paused.
     
  3. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    doesnt function!
    look at my other script
    Code (JavaScript):
    1. #pragma strict
    2. var Player: Transform;
    3. var SpeedRotate: float;
    4. var SpeedEnemy:float;
    5. var _time:float;
    6. var _MaxDistance:int;
    7. function Start () {
    8. _time=2;
    9. _MaxDistance=1;
    10. }
    11. function Update () {
    12. AnimationAI._animation=0;
    13. if(Player!=null){
    14.                 if(Vector3.Distance(Player.position,transform.position)<20){
    15.                         var rotate= Quaternion.LookRotation(Player.position - transform.position);
    16.                         //Rotaçao do inimigo
    17.                         transform.rotation=Quaternion.Slerp(transform.rotation,rotate,SpeedRotate*Time.deltaTime);
    18.                         //Movimentaçao  do inimigo
    19.                         if(Vector3.Distance(Player.position,transform.position)>_MaxDistance){
    20.                                 transform.Translate(0,0,SpeedEnemy*Time.deltaTime);
    21.                                 AnimationAI._animation=1;
    22.                              
    23.                         }
    24.              
    25.                 }
    26.                 if(Vector3.Distance(Player.position,transform.position)<1){
    27.                 _time+=1*Time.deltaTime;
    28.              
    29.                 if(_time>4){
    30.                 AnimationAI._animation=2;
    31.                 Attributes._hp+=-1;
    32.                 _time=0;
    33.                 }
    34.                 }
    35.         }
    36. }
    and the damage script
    Code (JavaScript):
    1.      function OnCollisionEnter(hit: Collision){
    2.          if(hit.gameObject.tag == "Player"){
    3.  
    4.          AnimationAI._animation=2;
    5.        
    6.  
    7.              Attributes._hp-=AIlife.damage;
    8.          }
    9.  
    10.     }
     
  4. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    how i put the animation without the update
     
  5. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    because there are no class names i have to guess. The First Code you postet is in your class AnimationAI ?
    Like Antony Blackett said you are starting your animations in every update. also you are setting your _animation integer in the second script every frame to 0.

    without update
    in your AnimationAI class

    Code (JavaScript):
    1. function PlayAnimation(_animation : int) {
    2.    switch(_animation){
    3.    case(0):
    4.    GetComponent.<Animation>().Play("idlee");
    5.    break;
    6.    case(1):
    7.    GetComponent.<Animation>().Play("correr");
    8.    break;
    9.    case(2):
    10.    GetComponent.<Animation>().Play("attackk");
    11.    case(3):
    12.    GetComponent.<Animation>().Play("waitingforbattle");
    13.    break;
    14.    case(4):
    15.    GetComponent.<Animation>().Play("diee");
    16.    break;
    17.    }
    18. }
    and than call your function in another script

    Code (JavaScript):
    1. AnimatorAI.PlayAnimation(2);
    hope syntax is correct, im more in C# than Javascript. can someone verify ?
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    that would be the next thing i would change. I'm also using Animator