Advertisement
HughesElite

Untitled

Apr 27th, 2020
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1.  
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6.  
  7. public class NPC : MonoBehaviour
  8. {
  9.  
  10.     //Initilise Navigation Mesh Agent
  11.     private UnityEngine.AI.NavMeshAgent agent;
  12.  
  13.     //Player Target for AI NPC
  14.     private GameObject target;
  15.  
  16.     //WayPoint Destination for AI NPC
  17.     protected Vector3 destPos;
  18.  
  19.     //WayPoints for AI NPC
  20.     GameObject[] pointList;
  21.  
  22.     //Store current & last NPC destination
  23.     private int navPointIndex;
  24.     private int lastNavPoint;
  25.  
  26.     //RayCase Length set to 20 units
  27.     private float rayLength = 20f;
  28.  
  29.  
  30.     //Store current NPC behaviour state
  31.     public States currentState;
  32.  
  33.     public enum States
  34.     {
  35.         NONE,
  36.         PATROL,
  37.         CHASE
  38.     }
  39.  
  40.  
  41.     //--------------------NPC Initilisation---------------//
  42.  
  43.     void Awake()
  44.     {
  45.  
  46.         //Set initial state to 'Patrol'
  47.         currentState = States.PATROL;
  48.  
  49.         //Get the list of points
  50.         pointList = GameObject.FindGameObjectsWithTag("WayPoint");
  51.  
  52.         //Set Random destination point
  53.         findNextPoint();
  54.  
  55.         //Setup Nav Mesh Agent
  56.         agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
  57.  
  58.         //Set Player as a target
  59.         target = GameObject.FindGameObjectWithTag("Player");
  60.     }
  61.  
  62.     //--------------------NPC Main Update----------------//
  63.  
  64.     void Update()
  65.     {
  66.         switch (currentState)
  67.         {
  68.             case States.PATROL:
  69.                 doPatrolUpdate();
  70.                 break;
  71.             case States.CHASE:
  72.                 doChaseUpdate();
  73.                 break;
  74.             default:
  75.                 break;
  76.         }
  77.     }
  78.  
  79.     //--------------------NPC Method Calls---------------//
  80.  
  81.     private void doPatrolUpdate()
  82.     {
  83.  
  84.         checkAtWaypoint();
  85.     }
  86.     private void doChaseUpdate()
  87.     {
  88.         checkPlayerTarget();
  89.     }
  90.  
  91.  
  92.     //--------------------NPC State Control---------------//
  93.  
  94.  
  95.     private void changeStateToPatrol()
  96.     {
  97.         currentState = States.PATROL;
  98.  
  99.     }
  100.     private void ChangeStateToChase()
  101.     {
  102.         currentState = States.CHASE;
  103.     }
  104.  
  105.     //--------------------NPC Methods--------------------//
  106.  
  107.     private void checkAtWaypoint()
  108.     {
  109.  
  110.         if (Vector3.Distance(this.transform.position, destPos) < 2f)
  111.         {
  112.             print("Reached the destination point & finding next");
  113.             findNextPoint();
  114.         }
  115.         else
  116.         {
  117.             playerInRange();
  118.         }
  119.  
  120.         //Update Navigation
  121.         agent.SetDestination(destPos);
  122.     }
  123.  
  124.  
  125.     private void playerInRange()
  126.     {
  127.         if (Vector3.Distance(transform.position, target.transform.position) <= 20f || canSeePlayer())
  128.         {
  129.             ChangeStateToChase();
  130.         }
  131.     }
  132.  
  133.     private void checkPlayerTarget()
  134.     {
  135.  
  136.         destPos = target.transform.position;
  137.  
  138.         float dist = Vector3.Distance(transform.position, destPos);
  139.  
  140.         if (dist >= 15.0f)
  141.         {
  142.  
  143.             changeStateToPatrol();
  144.             findNextPoint();
  145.  
  146.         }
  147.  
  148.         agent.SetDestination(destPos);
  149.     }
  150.  
  151.  
  152.     private void findNextPoint()
  153.     {
  154.         //While loop - until new nav point found not the same as previous
  155.  
  156.         while (navPointIndex == lastNavPoint)
  157.         {
  158.             navPointIndex = Random.Range(0, pointList.Length);
  159.             destPos = pointList[navPointIndex].transform.position;
  160.         }
  161.         //store current desitation in last navigation point
  162.         lastNavPoint = navPointIndex;
  163.     }
  164.  
  165.  
  166.  
  167.     private void OnDrawGizmos()
  168.     {
  169.         Vector3 fwd = transform.TransformDirection(Vector3.forward) * rayLength;
  170.  
  171.         switch (currentState)
  172.         {
  173.             case States.PATROL:
  174.                 Gizmos.color = Color.green;
  175.                 Debug.DrawRay(transform.position, fwd, Color.green);
  176.                 break;
  177.             case States.CHASE:
  178.                 Gizmos.color = Color.red;
  179.                 Debug.DrawRay(transform.position, fwd, Color.red);
  180.                 break;
  181.             default:
  182.                 break;
  183.         }
  184.  
  185.         Gizmos.DrawWireSphere(this.transform.position + (Vector3.up * 2), .5f);
  186.     }
  187.  
  188.     private bool canSeePlayer()
  189.  
  190.     {
  191.  
  192.         RaycastHit hit; //Check if raycast has hit Player tagged object
  193.  
  194.         if (Physics.Raycast(transform.position, transform.forward,
  195.             out hit, rayLength) && hit.collider.gameObject.tag == "Player")
  196.         {
  197.             return true; //yes
  198.         }
  199.         else
  200.         {
  201.             return false; //no
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement