Hi there,
I have animation of attack for my enemy as well as for move, idle and death. I want to make smth like that, what’s logically all in all, when animation is done then take damage of my player. With my script I can only set seconds between attacks, in addition it always hurts me when enemy get to me at first, so animation even didn’t start play yet. Here is whole script for attack of enemy:
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
Animator anim;
GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
bool playerInRange;
float timer;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
playerInRange = false;
}
}
void Update()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && playerInRange)
{
Attack();
anim.SetTrigger("Attacking");
}
if (playerHealth.currentHealth <= 0)
{
anim.SetTrigger("PlayerDead");
}
}
void Attack()
{
timer = 0f;
if (playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage);
anim.SetTrigger("Attacking");
}
}
}
And here is how my animator looks like if you would like to see this: