Search Unity

Playing attack animations once?

Discussion in 'Scripting' started by Daitomodachi, Jan 12, 2014.

  1. Daitomodachi

    Daitomodachi

    Joined:
    Nov 10, 2013
    Posts:
    10
    Hello everyone! I'm trying to write a code in which if the player is within attacking distance, an attack animation will play once and go into either idle or walk animation. I was wondering what's the typical way of accomplishing this? Here's the part of code I wrote thus far

    Code (csharp):
    1.  
    2.     // Handles whether the player is to move to a position or follow an enemy
    3.     void Player_Follow_Or_Move()
    4.     {
    5.         // Runs through the list of enemies on screen
    6.         foreach(GameObject Enemy in Enemies)
    7.         {
    8.             // Checks to see if the player clicked on the enemy
    9.             if(Physics2D.OverlapPoint(Player_Direction.GetPoint(Player_Distance),1 << LayerMask.NameToLayer("Enemies")))
    10.             {
    11.                 // Sets the selected game object as the target and updates position with position checker
    12.                 //Debug.Log("Enemy Selected"); 
    13.                 Target = Enemy;
    14.                 Position_Checker = Target.transform.position;
    15.  
    16.                 // Determines if the player is close enough for an attack
    17.                 if(Vector2.Distance (Position_Checker, transform.position)<.6f)
    18.                 {
    19.                     Debug.Log("Attack Enemy!");
    20.                     // Insert attacking code here
    21.  
    22.                 }
    23.  
    24.                 // Exits the main loop
    25.                 return;
    26.             }
    27.            
    28.         }
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.