Advertisement
Guest User

EnemyAI

a guest
Apr 9th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public abstract class EnemyAI : MonoBehaviour
  6. {
  7.  
  8.     public string hitTag;
  9.  
  10.     public bool lookingAtPlayer = false;
  11.     public bool isFiring        = false;
  12.     public bool alerted         = false;
  13.     public float fireRate       = 1.5f;
  14.  
  15.     public GameObject   enemy;
  16.     public Transform    player;
  17.  
  18.     public AudioSource[] alertSounds;
  19.     public int alertIndex = 0;
  20.  
  21.     public AudioSource attackSound;
  22.  
  23.  
  24.     void Update()
  25.     {
  26.         RaycastHit hit;
  27.         if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
  28.         {
  29.             hitTag = hit.transform.tag;          
  30.         }
  31.         if(hitTag == "Player" && isFiring == false)
  32.         {
  33.             StartCoroutine(EnemyFire());
  34.             if (!alerted)
  35.             {
  36.                 playAlertSound();
  37.                 alerted = true;
  38.             }
  39.         }
  40.         if(hitTag != "Player")
  41.         {
  42.             lookingAtPlayer = false;
  43.         }
  44.     }
  45.  
  46.     void playAlertSound()
  47.     {
  48.         System.Random rand = new System.Random();
  49.         alertIndex = rand.Next(1, alertSounds.Length);
  50.         alertSounds[alertIndex].Play();
  51.     }
  52.  
  53.     IEnumerator EnemyFire()
  54.     {
  55.         isFiring = true;
  56.         attackSound.Play();
  57.         lookingAtPlayer = true;
  58.         yield return new WaitForSeconds(Random.Range(.75f, 1.5f));
  59.         isFiring = false;
  60.     }
  61.  
  62.     public abstract void EnemyAttack();
  63.  
  64. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement