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:
public class MeleeTrigger : MonoBehaviour
{
private int meleeDmg = -20;
void OnTriggerEnter2D (Collider2D other)
{
if (other.isTrigger != true && other.CompareTag("Player_Njinja"))
{
HealthNjinja eHealth = other.gameObject.GetComponent<HealthNjinja>();
eHealth.ModifyHealth(meleeDmg);
}
}
}
This is also my meleeAttack code, but I don’t think here is the problem (this script is attached to the player):
public class MeleeAttack : MonoBehaviour
{
private bool Attacking = false;
private float attackTimer = 0.0f;
private float attackCD = 0.01f;
public Image meleeAttack;
public float cooldown = 2f;
bool isCooldown = false;
public Collider2D meleeTrigger;
public Animator animator;
void Start()
{
meleeAttack.fillAmount = 0;
}
void Awake()
{
animator = gameObject.GetComponent<Animator>();
meleeTrigger.enabled = false;
}
void Update()
{
if (Input.GetButtonDown("meleeAttack") && !Attacking && isCooldown == false)
{
isCooldown = true;
Attacking = true;
meleeAttack.fillAmount = 1;
FindObjectOfType<AudioManager>().Play("SwordAttack");
attackTimer = attackCD;
meleeTrigger.enabled = true;
}
if (isCooldown)
{
meleeAttack.fillAmount -= 1 / cooldown * Time.deltaTime;
if (meleeAttack.fillAmount <= 0)
{
meleeAttack.fillAmount = 0;
isCooldown = false;
}
}
if (Attacking)
{
if (attackTimer > 0)
{
attackTimer -= Time.deltaTime;
}
else
{
Attacking = false;
meleeTrigger.enabled = false;
}
}
animator.SetBool("IsAttacking", Attacking);
}
}