Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public abstract class EnemyAI : MonoBehaviour
- {
- public string hitTag;
- public bool lookingAtPlayer = false;
- public bool isFiring = false;
- public bool alerted = false;
- public float fireRate = 1.5f;
- public GameObject enemy;
- public Transform player;
- public AudioSource[] alertSounds;
- public int alertIndex = 0;
- public AudioSource attackSound;
- void Update()
- {
- RaycastHit hit;
- if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
- {
- hitTag = hit.transform.tag;
- }
- if(hitTag == "Player" && isFiring == false)
- {
- StartCoroutine(EnemyFire());
- if (!alerted)
- {
- playAlertSound();
- alerted = true;
- }
- }
- if(hitTag != "Player")
- {
- lookingAtPlayer = false;
- }
- }
- void playAlertSound()
- {
- System.Random rand = new System.Random();
- alertIndex = rand.Next(1, alertSounds.Length);
- alertSounds[alertIndex].Play();
- }
- IEnumerator EnemyFire()
- {
- isFiring = true;
- attackSound.Play();
- lookingAtPlayer = true;
- yield return new WaitForSeconds(Random.Range(.75f, 1.5f));
- isFiring = false;
- }
- public abstract void EnemyAttack();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement