Search Unity

Melee attack on trigger with collider

Discussion in '2D' started by Certifikat, Jul 26, 2020.

  1. Certifikat

    Certifikat

    Joined:
    Mar 24, 2019
    Posts:
    2
    I have strange bug with my melee attack. Most of the time my melee attack is not working.
    If im standing still and do the melee attack to the enemy, than maybe the first hit will do the damage,
    but rest of the hits not. I have recorded it to show what happens:

    I think the issue is the meleeTrigger script but I'm not sure.
    Script is attached to the gameObject meleeTrigger with 2D collider.
    Here is my code:

    Code (CSharp):
    1. public class MeleeTrigger : MonoBehaviour
    2.     {
    3.     private int meleeDmg = -20;
    4.     void OnTriggerEnter2D (Collider2D other)
    5.     {
    6.         if (other.isTrigger != true && other.CompareTag("Player_Njinja"))
    7.         {
    8.             HealthNjinja eHealth = other.gameObject.GetComponent<HealthNjinja>();
    9.             eHealth.ModifyHealth(meleeDmg);
    10.         }
    11.     }
    12. }
    This is also my meleeAttack code, but I don't think here is the problem (this script is attached to the player):

    Code (CSharp):
    1. public class MeleeAttack : MonoBehaviour
    2.     {
    3.     private bool Attacking = false;
    4.     private float attackTimer = 0.0f;
    5.     private float attackCD = 0.01f;
    6.     public Image meleeAttack;
    7.     public float cooldown = 2f;
    8.     bool isCooldown = false;
    9.     public Collider2D meleeTrigger;
    10.     public Animator animator;
    11.     void Start()
    12.     {
    13.         meleeAttack.fillAmount = 0;
    14.     }
    15.     void Awake()
    16.     {
    17.         animator = gameObject.GetComponent<Animator>();
    18.         meleeTrigger.enabled = false;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (Input.GetButtonDown("meleeAttack") && !Attacking && isCooldown == false)
    24.         {
    25.             isCooldown = true;
    26.             Attacking = true;
    27.             meleeAttack.fillAmount = 1;
    28.             FindObjectOfType<AudioManager>().Play("SwordAttack");
    29.             attackTimer = attackCD;
    30.             meleeTrigger.enabled = true;
    31.         }
    32.         if (isCooldown)
    33.         {
    34.             meleeAttack.fillAmount -= 1 / cooldown * Time.deltaTime;
    35.             if (meleeAttack.fillAmount <= 0)
    36.             {
    37.                 meleeAttack.fillAmount = 0;
    38.                 isCooldown = false;
    39.             }
    40.         }
    41.         if (Attacking)
    42.         {
    43.             if (attackTimer > 0)
    44.             {
    45.                 attackTimer -= Time.deltaTime;
    46.             }
    47.             else
    48.             {
    49.                 Attacking = false;
    50.                 meleeTrigger.enabled = false;
    51.             }
    52.         }
    53.         animator.SetBool("IsAttacking", Attacking);
    54.     }
    55. }
     
  2. Certifikat

    Certifikat

    Joined:
    Mar 24, 2019
    Posts:
    2
    I found a solution to this, and it worked perfectly. I used another script for it. Furthermore, I didn't use melee Trigger anymore, rather MeleePrefabKnightAdvanturer (attach it to prefab and make it invisible) and the button where I Instantiate(meleePrefab, firePointMeleeAttack.position, firePointMeleeAttack.rotation); After instantiate the prefab will be very fast destroyed, so it would be like melee attack. :D

    This is the game I developed from it: https://play.google.com/store/apps/details?id=com.CertiDevelopment.MontheeAdventure

    SCRIPTS

    Code (CSharp):
    1. public class MeleePrefabKnightAdvanturer  : MonoBehaviour  {
    2.     public Rigidbody2D rb;
    3.     public int damageDoneMeleeAttack; //it deals 40 damage close range, 20 damage long range
    4.     private readonly float speedOfMeleeAttack = 100f;
    5.  
    6.     void Start()
    7.     {
    8.         StartCoroutine(DestroyGameobject());
    9.         rb.velocity = transform.right * speedOfMeleeAttack;
    10.     }
    11.  
    12.         switch (other.tag)
    13.         {
    14.             case "Enemy_1":
    15.                 Enemy1 eHealth = other.gameObject.GetComponent<Enemy1Health>();
    16.                 eHealth.ModifyHealth(damageDoneMeleeAttack);
    17.                 break;
    18.             case "Enemy_2":
    19.                 Enemy2 eHealth2 = other.gameObject.GetComponent<Enemy2Health >();
    20.                 eHealth2.ModifyHealth(damageDoneMeleeAttack);
    21.                 break;
    22.         }
    23.  
    24.     IEnumerator DestroyGameobject()
    25.     {
    26.         yield return new WaitForSeconds(0.09f);
    27.         Destroy(gameObject);
    28.     }
    29. }

    Code (CSharp):
    1. public class MeleeAttackButton : MonoBehaviour
    2. {
    3.  
    4.     public Transform firePointMeleeAttack;
    5.     public GameObject meleePrefab;
    6.     public Animator animator;
    7.     public Image shootingMeleeAttack;
    8.     public MeleePrefabKnightAdvanturer meleeDamage;
    9.     private readonly float coolddownMeleeAttacking = 0.5f;
    10.     private bool isCooldownMeleeAttack = false;
    11.     private bool isMeleeAttacking = false;
    12.     private float maleeAttackTimer = 0.0f;
    13.     private float maleeAttackSpeed = 0.01f;
    14.     private bool isPressed;
    15.  
    16.     void Start()
    17.     {
    18.         shootingMeleeAttack.fillAmount = 0;
    19.         isPressed = false;
    20.     }
    21.     public void Update()
    22.     {
    23.         if (isPressed && isCooldownMeleeAttack == false)
    24.         {
    25.             Instantiate(meleePrefab, firePointMeleeAttack.position, firePointMeleeAttack.rotation);
    26.             isCooldownMeleeAttack = true;
    27.             isMeleeAttacking = true;
    28.             shootingMeleeAttack.fillAmount = 1;
    29.             maleeAttackTimer = maleeAttackSpeed;
    30.             ShootMeleeAttack();
    31.             FindObjectOfType<AudioManager>().Play("SwordAttack");
    32.         }
    33.         isPressed = false;
    34.         if (isCooldownMeleeAttack)
    35.         {
    36.             shootingMeleeAttack.fillAmount -= 1 / coolddownMeleeAttacking * Time.deltaTime;
    37.             if (shootingMeleeAttack.fillAmount <= 0)
    38.             {
    39.                 shootingMeleeAttack.fillAmount = 0;
    40.                 isCooldownMeleeAttack = false;
    41.             }
    42.         }
    43.         if (isMeleeAttacking)
    44.         {
    45.             if (maleeAttackTimer > 0)
    46.             {
    47.                 maleeAttackTimer -= Time.deltaTime;
    48.             }
    49.             else
    50.             {
    51.                 isMeleeAttacking = false;
    52.             }
    53.         }
    54.         animator.SetBool("IsAttacking", isMeleeAttacking);
    55.     }
    56.     public void ShootMeleeAttack()
    57.     {
    58.         isPressed = true;    
    59.     }
    60.  
    61. }