- Home /
Enemy's initial attack doesn't deal damage to player
Good evening, I'm having an issue where any enemy my player first encounters attacks, the first attack doesn't deal damage. However, subsequent attacks after that register the damage. I'm not quite sure why and was wondering if someone could help. Thank you! Here's my code for the enemy attacks: private int damageAmount; private int attackNum = 1; //attack index private Animator anim; private bool inAttackRange; private bool isNear; private float timeBtwAttack; private float currentDistance;
[SerializeField] private float distance = 2f;
[SerializeField] private float startTimeBtwAttack;
[SerializeField] private Transform attackPos;
[SerializeField] private LayerMask whoIsPlayer;
[SerializeField] private float attackRangeX, attackRangeY;
[SerializeField] private int attack1, attack2, attack3;
private EnemyPatrol patrol;
private void Awake()
{
anim = GetComponent<Animator>();
patrol = GetComponent<EnemyPatrol>();
timeBtwAttack = startTimeBtwAttack;
}
private void Update()
{
//is player in attack range
inAttackRange = Physics2D.OverlapBox(attackPos.position, new Vector2(attackRangeX, attackRangeY), 0, whoIsPlayer);
currentDistance = Mathf.Abs(transform.position.x - PlayerHealth.instance.transform.position.x);
//check if distance from player is near
if(currentDistance > distance)
{
isNear = false;
} else
{
isNear = true;
}
if (timeBtwAttack <= 0)
{
if (inAttackRange && isNear)
{
patrol.enabled = false;
//DealDamage();
anim.Play("attack" + attackNum);
timeBtwAttack = startTimeBtwAttack;
}
else
{
patrol.enabled = true;
}
}
else
{
timeBtwAttack -= Time.deltaTime;
}
}
private void DealDamage()
{
if (isNear)
{
PlayerHealth.instance.TakeDamage(damageAmount);
}
if (attackNum == 1)
{
damageAmount = attack1;
}
else if (attackNum == 2)
{
damageAmount = attack2;
}
else if (attackNum == 3)
{
damageAmount = attack3;
}
attackNum++;
if (attackNum > 3)
{
attackNum = 1;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(attackPos.position, new Vector2(attackRangeX, attackRangeY));
}
Answer by W4uLi · Mar 22 at 02:08 AM
@Allurance Your AttackNum is set to 1 at the start but if you call DealDamage() it will be put to 2 because of the attackNum++ that gets called without restriction. Set AttackNum to 0 at the start and when attackNum > 3
You could also clean it up a bit more and swap those ifs with a switch case like:
private void DealDamage()
{
if (isNear)
{
switch (attackNum)
{
case 1:
damageAmount = attack1;
break;
case 2:
damageAmount = attack2;
break;
case 3:
damageAmount = attack3;
attackNum = 0;
break;
}
PlayerHealth.instance.TakeDamage(damageAmount);
attackNum++;
}
}
Hope that works and I didn't mess up somewhere.
It's giving me a warning of "Invalid Layer Index '-1' UnityEngine.Animator:Play (string)
Check that your animations are named the same as they are in the animator controller. Also maybe you want to put the anim.Play at the bottom of the DealDamage function but before the attacknum++
I appreciate your help, I discovered that Unity was trying to play the attack animation before attackNum was incremented so attackNum was still 0 when playing an animation which doesn't exist. I ended up setting attackNum to 1 at the start and checked if before the animations were played which fixed it. Thank you for your help.
Your answer
Follow this Question
Related Questions
Shade Cloak in Unity 1 Answer
Help Me! I want to make a pixel fairy! 1 Answer
Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers