Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class NPC : MonoBehaviour
- {
- //Initilise Navigation Mesh Agent
- private UnityEngine.AI.NavMeshAgent agent;
- //Player Target for AI NPC
- private GameObject target;
- //WayPoint Destination for AI NPC
- protected Vector3 destPos;
- //WayPoints for AI NPC
- GameObject[] pointList;
- //Store current & last NPC destination
- private int navPointIndex;
- private int lastNavPoint;
- //RayCase Length set to 20 units
- private float rayLength = 20f;
- //Store current NPC behaviour state
- public States currentState;
- public enum States
- {
- NONE,
- PATROL,
- CHASE
- }
- //--------------------NPC Initilisation---------------//
- void Awake()
- {
- //Set initial state to 'Patrol'
- currentState = States.PATROL;
- //Get the list of points
- pointList = GameObject.FindGameObjectsWithTag("WayPoint");
- //Set Random destination point
- findNextPoint();
- //Setup Nav Mesh Agent
- agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
- //Set Player as a target
- target = GameObject.FindGameObjectWithTag("Player");
- }
- //--------------------NPC Main Update----------------//
- void Update()
- {
- switch (currentState)
- {
- case States.PATROL:
- doPatrolUpdate();
- break;
- case States.CHASE:
- doChaseUpdate();
- break;
- default:
- break;
- }
- }
- //--------------------NPC Method Calls---------------//
- private void doPatrolUpdate()
- {
- checkAtWaypoint();
- }
- private void doChaseUpdate()
- {
- checkPlayerTarget();
- }
- //--------------------NPC State Control---------------//
- private void changeStateToPatrol()
- {
- currentState = States.PATROL;
- }
- private void ChangeStateToChase()
- {
- currentState = States.CHASE;
- }
- //--------------------NPC Methods--------------------//
- private void checkAtWaypoint()
- {
- if (Vector3.Distance(this.transform.position, destPos) < 2f)
- {
- print("Reached the destination point & finding next");
- findNextPoint();
- }
- else
- {
- playerInRange();
- }
- //Update Navigation
- agent.SetDestination(destPos);
- }
- private void playerInRange()
- {
- if (Vector3.Distance(transform.position, target.transform.position) <= 20f || canSeePlayer())
- {
- ChangeStateToChase();
- }
- }
- private void checkPlayerTarget()
- {
- destPos = target.transform.position;
- float dist = Vector3.Distance(transform.position, destPos);
- if (dist >= 15.0f)
- {
- changeStateToPatrol();
- findNextPoint();
- }
- agent.SetDestination(destPos);
- }
- private void findNextPoint()
- {
- //While loop - until new nav point found not the same as previous
- while (navPointIndex == lastNavPoint)
- {
- navPointIndex = Random.Range(0, pointList.Length);
- destPos = pointList[navPointIndex].transform.position;
- }
- //store current desitation in last navigation point
- lastNavPoint = navPointIndex;
- }
- private void OnDrawGizmos()
- {
- Vector3 fwd = transform.TransformDirection(Vector3.forward) * rayLength;
- switch (currentState)
- {
- case States.PATROL:
- Gizmos.color = Color.green;
- Debug.DrawRay(transform.position, fwd, Color.green);
- break;
- case States.CHASE:
- Gizmos.color = Color.red;
- Debug.DrawRay(transform.position, fwd, Color.red);
- break;
- default:
- break;
- }
- Gizmos.DrawWireSphere(this.transform.position + (Vector3.up * 2), .5f);
- }
- private bool canSeePlayer()
- {
- RaycastHit hit; //Check if raycast has hit Player tagged object
- if (Physics.Raycast(transform.position, transform.forward,
- out hit, rayLength) && hit.collider.gameObject.tag == "Player")
- {
- return true; //yes
- }
- else
- {
- return false; //no
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement