- Home /
I need help making my enemy AI have an attack delay
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class EnemyAttack : MonoBehaviour { public int nulldamage = 0; public float attackDelay = 10; float lastAttacked = -999;
public int attackDamage = 2; Animator anim; GameObject player; PlayerHealth playerHealth; //float timer; //public float attackRate = 0f; //EnemyHealth enemyHealth;
// 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 OnTriggerStay(Collider other)
{
if (other.gameObject == player)
{
Attack();
}
}
void Update()
{
if (playerHealth.currentHealth <= 0)
{
Destroy(player);
anim.SetTrigger("PlayerDead");
}
}
private void Attack() //Call this in OnTriggerStay if the colliding gameObject is the player
{
while (Time.time > lastAttacked + attackDelay)
{
anim.SetTrigger("Attack"); //trigger our animation
if (playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage); //make player take damage
}
lastAttacked = Time.time;
}
}
}
Please clarify your issue. This script could work if the player enter the Trigger attached to the Enemy (which would call OnTriggerStay).
The issue right now is that when my player enters the collider the enemy attacks but it attacks really fast and my health drops from 100 to 0 pretty much instantly im wondering how i would make the delay be longer so i dont die instantly
$$anonymous$$y problem is that when the player enters the collider the enemy attacks but when it attacks its really fast and my player health drops from 100 to 0 and the player dies instantly with any chance is there some way to make it so the the time between the attacks or damage dealt to the player is takes longer to kill the player would i have to make the damage dealt really small to where it's a decimal or is the some way to make it so the player takes the damage but at a slower rate
Answer by tormentoarmagedoom · May 08, 2018 at 04:00 PM
Good day.
You need to create a cooldown timer, and atack only if cooldown has reached 0.
something like this...
float TimerForNextAttack, Cooldown;
vois Start()
{
Cooldown = 3;
TimerForNextAttack = Cooldown;
void Update()
{
if (TimerForNextAttack > 0)
{
TimerForNextAttack -= Time.deltaTime;
}
else if (TimerForNextAttack <=0)
{
Atack();
TimerForNextAttack = Cooldown;
}
}
Bye!
Answer by anthot4 · May 08, 2018 at 01:00 PM
On Line 15 you could have:
Invoke("Attack",2.0f); // instead of just calling attack. This calls the attack function in 2 seconds.
Or if you want something more random:
var Delay = Random.Range(1,3); // choose a number between 1 and 3 to wait for the enemy to attack
Invoke("Attack",Delay);
thx for this it helped with the initial delay before the attack but what im more trying to get is the delay between each attack when the player has damage being dealt to them because when the player is in range it starts to attack but it attacks at a really fast rate is there some way to make the delay between attacks longer as well
Your answer
Follow this Question
Related Questions
Creating basic enemy animation and attacking 4 Answers
Multiple Enemy Collision Help 0 Answers
How to make an enemy attack every few seconds 1 Answer
Enemy AI problems 2 Answers
Problem with enemy AI 1 Answer