Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting the length of an animation

Discussion in 'Scripting' started by moynzy, Nov 27, 2014.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    So hey guys, i'm trying to implement some melee with animation.


    Code (CSharp):
    1.         if(target != null && target.name == "Player"){
    2.             baseMeleeTimer -= Time.deltaTime;
    3.  
    4.             float dist = Vector3.Distance(target.position, myTransform.position);
    5.             if(dist > 1.0f && dist < 3.9f){
    6.                 //Debug.Log("We can attack" + dist);
    7.                 //baseMeleeTimer -= Time.deltaTime;
    8.                 Debug.Log(baseMeleeTimer);
    9.                 if(baseMeleeTimer < meleeResetTimer){
    10.                     Debug.Log("Attack");
    11.                     Debug.Log(baseMeleeTimer);
    12.                     MeleeAttack();
    13.                 }
    14.             }
    15.  
    16.         }
    So basically, if the player is targeted the timer ticks down. If the player is in that ditance, and the baseMeleeTimer is less than 0, then it goes to the MeleeAttack();

    Code (CSharp):
    1.     private void MeleeAttack(){
    2.         Debug.Log("Attacking");
    3.         if(animation.IsPlaying("idle"))
    4.         {
    5.             //Debug.Log("Dont play");
    6.             animation.Stop("idle");
    7.             animation.Play("attack3");
    8.             if(animation["attack3"].length > 1.0f){
    9.                 //Debug.Log("Stop attacking");
    10.                 animation.Stop();
    11.             }
    12.         }
    13.     }
    now I check to see if idle is playing. If is then I stop this animation and play the attack animation.

    NOW HERE - i want to check if that animation has stopped playing. If it has, then I'll stop the animation and set the timer back to 0 , and do some collision testing (god help me).

    So the question is, is there a method where it can return the length of an animation so I can use it in my "If" statement.

    Thank you for your time.
     
  2. Peter Ples

    Peter Ples

    Joined:
    Mar 12, 2013
    Posts:
    237