Take damage only when attack animation is done?
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:
Answer by Veerababu.g · Jun 13, 2016 at 04:50 AM
here what you have to do is you have to execute the damage code after animation completed. unity provides you one method that is executed whenever the animation completed. to do this go to animation window there is a small icon (add event) click that button and add your damage method to that.that's it you are done
I'm noob and can tell me how to go into this animation for attack in animation window? I was trying for like 15 $$anonymous$$utes and I don't know how to get there XD And as second how should animator look like?