Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- using Opsive.UltimateCharacterController.Traits;
- public class ArtificialIntelligence : MonoBehaviour {
- private EnemySpawner enemySpawner;
- private CharacterHealth hp;
- private Animator anim;
- private NavMeshAgent agent;
- private FieldOfView fov;
- [SerializeField]
- private GameObject[] players;
- public List<Transform> playersList = new List<Transform>();
- public Transform closestPlayer;
- public bool seeingPlayer;
- public Waypoints waypoints;
- [SerializeField]
- private Transform nextWaypoint;
- //public bool movingToWaypoint = false;
- //public float distanceToPlayer;
- //private float timeToAttack = 0f;
- //public GameObject impactPoint;
- // Use this for initialization
- void Start () {
- fov = GetComponent<FieldOfView>();
- waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();
- enemySpawner = GameObject.Find("WaveSpawner").GetComponent<EnemySpawner>();
- players = GameObject.FindGameObjectsWithTag("Player");
- for (int i = 0; i < players.Length; i++)
- {
- playersList.Add(players[i].transform);
- }
- hp = GetComponent<CharacterHealth>();
- agent = GetComponent<NavMeshAgent>();
- anim = GetComponent<Animator>();
- StartCoroutine("GoToNextWaypoint");
- }
- // Update is called once per frame
- void Update ()
- {
- if(seeingPlayer)
- {
- closestPlayer = fov.GetClosestEnemy(playersList.ToArray());
- }
- else
- {
- closestPlayer = null;
- }
- if (closestPlayer != null)
- {
- ChaseTarget();
- }
- if (hp.HealthValue <= 0)
- {
- RemoveEnemyFromList();
- }
- }
- void ChaseTarget()
- {
- agent.SetDestination(closestPlayer.position);
- float playerDistance = Vector3.Distance(transform.position, closestPlayer.position);
- if(playerDistance > 30f)
- {
- seeingPlayer = false;
- StartCoroutine("GoToNextWaypoint");
- }
- }
- public IEnumerator GoToNextWaypoint()
- {
- anim.SetBool("Following", false);
- anim.SetBool("Run", false);
- yield return new WaitForSeconds(2.5f);
- nextWaypoint = waypoints.waypoints[Random.Range(0, waypoints.waypoints.Length)].transform;
- agent.SetDestination(nextWaypoint.position);
- if (agent.velocity.magnitude > 3f)
- {
- anim.SetBool("Following", true);
- anim.SetBool("Run", true);
- anim.SetBool("Attack", false);
- }
- else
- {
- anim.SetBool("Following", true);
- anim.SetBool("Run", false);
- anim.SetBool("Attack", false);
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.tag == "Waypoint")
- {
- StartCoroutine("GoToNextWaypoint");
- }
- }
- public void RemoveEnemyFromList()
- {
- enemySpawner.SendMessage("RemoveEnemy", this.gameObject);
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement