- Home /
Detect enemy on animation
So, I need to make a damage to an enemy after player touches ENEMY with his fists or weapon(in my game I can change weapons, like swords and etc.) It is really cool with enemies: I get damage after animation event and they attack me when I am close and I die after while. But I can't do same with player, player attacks, animation event is working but I can't make any damage(make HP go to zero). How to make animation collider(if it exists), how to damage enemies with animations? relevant parts of code:
private void Attackin()
{
if (Input.GetButtonDown("Fire1"))
{
StartCoroutine("playerAttackAnim");
}
}
void Hit()//animation event
{
Debug.Log("Player attack is working");
if (enemyHealth == null) { Debug.Log("No Enemy Detected"); return; }
//it is working
enemyHealth.TakeDamage(gameObject, damage);//not working against enemies
}
Get Damage(Health script):
public void TakeDamage(GameObject instigator,float damage)
{
Debug.Log(gameObject.name + "took " + damage + " Damage");
healthPoints = Mathf.Max(healthPoints - damage, 0);
if(healthPoints <= 0)
{
Die();
}
}
Answer by conguerror · Feb 01, 2020 at 05:03 PM
Answer:
void Hit()//animation event
{
Debug.Log("Player attack is working");
//it is working
Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayer);
foreach (Collider enemy in hitEnemies)
{
Debug.Log("I hit enemy ");
enemy.GetComponent<Health>().TakeDamage(gameObject,damage);
}
}
Your answer
Follow this Question
Related Questions
weapon destroyes enemy without running animation -1 Answers
How To Make Enemy Chase You? 1 Answer
Damage script is screwed up...? what to do? 1 Answer
Enemy animation help? 1 Answer
Is there a bug with animation.isplaying 0 Answers