//right now its stuck looping the attack animation at all times i cant tell if its a coding problem exactly or a animator controller set up problem help if possible.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
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;
// Use this for initialization
void Awake ()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth> ();
//enemyHealth = GetComponent<EnemyHealth> ();
anim = GetComponent <Animator> ();
}
// Update is called once per frame
void OnTriggerEnter (Collider other)
{
if(other.gameObject == player)
{
playerInRange = true;
anim.SetTrigger("Attack");
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject == player)
{
playerInRange = false;
}
}
void Update()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && playerInRange/* && enemyHealth.currentHealth > 0*/)
{
Attack();
}
if (playerHealth.currentHealth <= 0)
{
Destroy(player);
}
}
void Attack()
{
timer = 0f;
if(playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage);
}
}
}