Search Unity

Attack Cooldown and Animation

Discussion in 'Scripting' started by SpiderPig1660, Oct 20, 2020.

  1. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    I need help with attack cooldown because it my first time using attack cooldown. I want the player character to play the attack animation when the attack speed is less than 0, but when the attack speed is less than 0, it instantly change to animation ("Attack", false). The second problem is I want the character to have no attack cooldown when it first attack/when it spawn, after the second attack it have attack cooldown.
    Here is my code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Collections.Specialized;
    5. using System.Security.Cryptography;
    6. using System.Security.Cryptography.X509Certificates;
    7. using UnityEngine;
    8.  
    9. public class PlayerAIBehaviour : MonoBehaviour
    10. {
    11.     public float playermaxhealth; //maxhealth
    12.     public float playercurrenthealth; //currenthealth
    13.     public float speed; //speed
    14.     public float attackrange; //attackrange
    15.     public float attackspeed;
    16.     public float attackcooldown;
    17.     private Transform enemy;
    18.     private Animator anim;
    19.     Rigidbody2D rb;
    20.     bool moving;
    21.  
    22.     void Start()
    23.     {
    24.         rb = GetComponent<Rigidbody2D>();
    25.         enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
    26.         anim = GetComponent<Animator>();
    27.         playercurrenthealth = playermaxhealth;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         {
    34.             if (moving == true)
    35.             {
    36.                 transform.position += Vector3.right * speed * Time.deltaTime;
    37.             }
    38.             else if (moving == false)
    39.             {
    40.                 transform.position += Vector3.right * 0 * Time.deltaTime;
    41.             }
    42.             if (enemy == null && anim.GetBool("Attack") == true) //when enemy died, keep moving
    43.                 anim.SetBool("Attack", false);
    44.             moving = true;
    45.  
    46.             if (enemy == null)
    47.                 return;
    48.         }
    49.         float distanceFromEnemy = Vector2.Distance(enemy.position, transform.position);
    50.  
    51.         if (distanceFromEnemy > attackrange) //AI moving
    52.         {
    53.             moving = true;
    54.             anim.SetBool("Moving", true);
    55.             anim.SetBool("Attack", false);
    56.         }
    57.         if (attackspeed <= 0) //Attacking
    58.         {
    59.             if (distanceFromEnemy < attackrange)
    60.             {
    61.                 moving = false;
    62.                 anim.SetBool("Moving", false);
    63.                 anim.SetBool("Attack", true);
    64.             }
    65.             attackspeed = attackcooldown;
    66.         }
    67.         else if (attackspeed >= 0) //waiting for attackcooldown to recharge
    68.         {
    69.             if (distanceFromEnemy < attackrange)
    70.             {
    71.                 moving = false;
    72.                 anim.SetBool("Moving", false);
    73.                 anim.SetBool("Attack", false);
    74.             }
    75.         }
    76.         attackspeed -= Time.deltaTime;
    77.     }
    78.  
    79.     public void DoesDamage(float damage) //EnemyTakingDamage
    80.     {
    81.         anim.SetTrigger("Attack");  
    82.         playercurrenthealth -= damage;
    83.         if (playercurrenthealth <= 0)
    84.         {
    85.             Destroy(this.gameObject);
    86.         }
    87.     }
    88.  
    89.     public void SetMaxHealth()
    90.     {
    91.         playercurrenthealth = playermaxhealth;
    92.     }
    93.  
    94.     void OnTriggerEnter2D(Collider2D other)
    95.     {
    96.         if (other.tag == "Enemy")
    97.         {
    98.             anim.SetBool("Moving", true);
    99.         }
    100.     }
    101.  
    102.     void OnTriggerExit2D(Collider2D other)
    103.     {
    104.         if (other.tag == "Enemy")
    105.         {
    106.             anim.SetBool("Moving", false);
    107.         }
    108.     }
    109.  
    110.     public void OnDrawGizmosSelected()
    111.     {
    112.         Gizmos.color = Color.red;
    113.         Gizmos.DrawWireSphere(transform.position, attackrange);
    114.     }
    115. }
    116.  
    117.  
    118.  
    For the first problem, I think I need to change the line 67 to something else, but I don't know what to change it to.
    If anyone have a better way to use attack cooldown, let me know.
    Note: I'm making a game similar to Line Ranger/Battle Cat, if anyone curious.
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    For your first problem: you check on both statements if attackspeed is 0, remove the = from teh else if and just check if it is bigger than 0:
    else if (attackspeed > 0)


    Put your "attackspeed = attackcooldown;" in the if where you check, if the enemy is in range (move it to line 64) and call your "DoesDamage" method before, then the cooldown will be set if the enemy is in range and after damage was done.

    Otherwise you could use a flag to check if the first attack was done, but then you would have at least on more if statement
     
    SpiderPig1660 likes this.
  3. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    The animation doesn't change instantly now but it keep attacking forever.
    Thanks for the help :D
     
  4. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    Yes, because you set the attackspeed after each attack to it´s start value:

    attackspeed = attackcooldown;
     
    SpiderPig1660 likes this.
  5. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    OMG! the animation is fix now! all I had to do is check has exit time from attack to idle!