Question by
taix97 · Mar 12, 2019 at 04:30 PM ·
animationanimatorenemyenemy health
Sync animation with damage deal
Hello, I have a problem. I can't sync animation and making damage. I'm using Animation Events, but everytime i press the button for attack, it takes damade. Thanks
This is the AI health script
public int enemyHealth;
public int damage = 1;
public Animator animator;
public bool stay;
public bool isDead = false;
private void Start()
{
animator = GetComponent<Animator>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.X) && stay)
{
enemyHealth -= damage;
}
if (enemyHealth <= 0)
{
animator.SetBool("isDead", true);
isDead = true;
Destroy(gameObject, 2f);
}
}
void OnTriggerStay2D(Collider2D collision)
{
if (collision.tag == "Sword")
{
stay = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
stay = false;
}
}
And this is what I add to Animation event.
public void PlayerAttack()
{
if (Input.GetKeyDown(KeyCode.X))
{
animator.SetBool("Attack", true);
}
if (Input.GetKeyUp(KeyCode.X))
{
animator.SetBool("Attack", false);
}
}
Comment