Reputation: 1
So when I click the attack button, the "sword" is meant to swing and damage an enemy for 34 damage. What actually happens is that the sword swings and damaged the enemy for something around 1000 damage! Could someone please help.
public Transform attackPoint;
public float attackRange = 0.05f;
public LayerMask enemyLayers;
public int attackDamage = 34;
if (Input.GetButton("Fire1"))
{
Attack();
}
void Attack()
{
animator.SetTrigger("Attack");
Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);
foreach(Collider enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
And this is the enemy script:
public int maxHealth = 100;
int currentHealth;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("died");
}
Thank you!
Upvotes: 0
Views: 285
Reputation: 116
Would I be correct in assuming the if statement that calls the Attack method is in a Update method?
If so what is happening is Unity is calling the Attack method in every frame the button is held down, not just once when you first press it. You should replace Input.GetButton with either Input.GetButtonDown if you want the attack to happen when the button is first pressed or Input.GetButtonUp if you want the attack to happen when the button is released. Cheers!
Upvotes: 4