- Home /
 
How do i make my enemy AI attack?
So i'm really new and i want that my Enemy would attack me and do a certain amount of damage (would be nice if there was a range and delay)
Here's my Enemy script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class EnemyAII : MonoBehaviour {
 Transform playerTransform;
 UnityEngine.AI.NavMeshAgent myNavmesh;
 public float checkRate = 0.01f;
 float nextCheck;
 // Start is called before the first frame update
 void Start() {
     if (GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
     myNavmesh = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();       
     }
 // Update is called once per frame
 void Update()
 {
     if (Time.time > nextCheck)
     {
         nextCheck = Time.time + checkRate;
         FollowPlayer();
     }
 }
 void FollowPlayer()
 {
     myNavmesh.transform.LookAt(playerTransform);
     myNavmesh.destination = playerTransform.position;
 }
 
               }
And here's my playerHealth script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; public int currentHealth;
 public HealthBar healthBar;
 // Start is called before the first frame update
 void Start()
 {
     currentHealth = maxHealth;
     healthBar.SetMaxHealth(maxHealth);
 }
 // Update is called once per frame
 void Update()
 {
     if // I don't know what to do here
     {
         TakeDamage(20);
     }
     if (currentHealth <= 0)
     {
         Die();
     }
 }    
 void Die()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }
 void TakeDamage(int damage)
 {
     currentHealth -= damage;
     healthBar.SetHealth(currentHealth);
 }
 
               }
So basicly i want the enemy do damage until my health bar is at 0.
Answer by ahsen35813 · Mar 29, 2020 at 04:53 PM
My answer is completely untested, and might not be very good, but I think I have a solution To cause damage to the player, maybe you could use void OnTriggerEnter(Collider other){}to detect when the enemy makes contact with the player. Then you would need to add a trigger collider around the enemy for the enemy's attack range, so that the player will take damage when in that range. The script could be something like this:
 public class EnemyAttack : MonoBehaviour
 {
 
     public float enemyCooldown = 1;
     public float damage = 1;
 
     private bool playerInRange = false;
     private bool canAttack = true;
 
     private void Update()
     {
         if (playerInRange && canAttack)
         {
             GameObject.Find("Player").GetComponent<ControllerForPlayer>().currentHealth -= damage;
             StartCoroutine(AttackCooldown());
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Player"));
          {
             playerInRange = true;
         }
     }
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Player"));
          {
             playerInRange = false;
         }
     }
     IEnumerator AttackCooldown()
     {
         canAttack = false;
         yield return new WaitForSeconds(enemyCooldown);
         canAttack = true;
     }
 }
 
 
               Make sure to make the currentHealth variable in the player script public and everything else should work. IMPORTANT: DO NOT forget to add a trigger collider to the enemy to specify it's attack radius.
I really hope this works for you. Please let me know if it does! This was a really fun thing to try and figure out, so thanks for that.
EDIT: I've revised this code a couple of times now, and I can verify that it does indeed work for me, so hopefully it will work for you too.
I get this error: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
i'm trying to fix it.
I change the float damage to int damage. The error is fixed, but i'm not taking damage
I'm so sorry. I didn't attach the script to the enemy xD It works Thanks!!!!!
lol. I did the same thing when I was testing this. I forgot to attach the script to the enemy. I'm glad it works for you!
what to write instead of ? cuz is giving me error on this line pls im really new
Your answer
 
             Follow this Question
Related Questions
FPS , not attackking if the player behind ? 0 Answers
fps shooting enemy 1 Answer
Ai Alien Zombies get stuck on trees? 0 Answers
how do I edit enemy stats 3 Answers
Enemy follows player then attacks! 2 Answers