Advertisement
Mortenssen

Untitled

Sep 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using Opsive.UltimateCharacterController.Traits;
  6.  
  7. public class ArtificialIntelligence : MonoBehaviour {
  8.  
  9.     private EnemySpawner enemySpawner;
  10.     private CharacterHealth hp;
  11.     private Animator anim;
  12.     private NavMeshAgent agent;
  13.     private FieldOfView fov;
  14.  
  15.     [SerializeField]
  16.     private GameObject[] players;
  17.     public List<Transform> playersList = new List<Transform>();
  18.     public Transform closestPlayer;
  19.  
  20.     public bool seeingPlayer;
  21.  
  22.  
  23.     public Waypoints waypoints;
  24.     [SerializeField]
  25.     private Transform nextWaypoint;
  26.     //public bool movingToWaypoint = false;
  27.     //public float distanceToPlayer;
  28.  
  29.     //private float timeToAttack = 0f;
  30.     //public GameObject impactPoint;
  31.  
  32.     // Use this for initialization
  33.     void Start () {
  34.         fov = GetComponent<FieldOfView>();
  35.         waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();
  36.         enemySpawner = GameObject.Find("WaveSpawner").GetComponent<EnemySpawner>();
  37.  
  38.         players = GameObject.FindGameObjectsWithTag("Player");
  39.  
  40.         for (int i = 0; i < players.Length; i++)
  41.         {
  42.             playersList.Add(players[i].transform);
  43.         }
  44.  
  45.         hp = GetComponent<CharacterHealth>();
  46.         agent = GetComponent<NavMeshAgent>();
  47.         anim = GetComponent<Animator>();
  48.  
  49.         StartCoroutine("GoToNextWaypoint");
  50.     }
  51.    
  52.     // Update is called once per frame
  53.     void Update ()
  54.     {
  55.         if(seeingPlayer)
  56.         {
  57.             closestPlayer = fov.GetClosestEnemy(playersList.ToArray());
  58.         }
  59.         else
  60.         {
  61.             closestPlayer = null;
  62.         }
  63.  
  64.         if (closestPlayer != null)
  65.         {
  66.             ChaseTarget();
  67.         }
  68.  
  69.         if (hp.HealthValue <= 0)
  70.         {
  71.             RemoveEnemyFromList();
  72.         }
  73.     }
  74.  
  75.     void ChaseTarget()
  76.     {
  77.         agent.SetDestination(closestPlayer.position);
  78.         float playerDistance = Vector3.Distance(transform.position, closestPlayer.position);
  79.  
  80.         if(playerDistance > 30f)
  81.         {
  82.             seeingPlayer = false;
  83.             StartCoroutine("GoToNextWaypoint");
  84.         }
  85.     }
  86.     public IEnumerator GoToNextWaypoint()
  87.     {
  88.         anim.SetBool("Following", false);
  89.         anim.SetBool("Run", false);
  90.         yield return new WaitForSeconds(2.5f);
  91.         nextWaypoint = waypoints.waypoints[Random.Range(0, waypoints.waypoints.Length)].transform;
  92.         agent.SetDestination(nextWaypoint.position);
  93.  
  94.         if (agent.velocity.magnitude > 3f)
  95.         {
  96.             anim.SetBool("Following", true);
  97.             anim.SetBool("Run", true);
  98.             anim.SetBool("Attack", false);
  99.         }
  100.         else
  101.         {
  102.             anim.SetBool("Following", true);
  103.             anim.SetBool("Run", false);
  104.             anim.SetBool("Attack", false);
  105.         }
  106.     }
  107.  
  108.     private void OnTriggerEnter(Collider other)
  109.     {
  110.         if (other.gameObject.tag == "Waypoint")
  111.         {
  112.             StartCoroutine("GoToNextWaypoint");
  113.         }
  114.     }
  115.  
  116.     public void RemoveEnemyFromList()
  117.     {
  118.         enemySpawner.SendMessage("RemoveEnemy", this.gameObject);
  119.         Destroy(gameObject);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement