Advertisement
GoodNoodle

AiController

Jul 9th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AIController : MonoBehaviour
  6. {
  7.     public AIController instance;
  8.     public float chaseDis = 5f;
  9.     float timeSinceLastSawPlayer = Mathf.Infinity;
  10.     float waypointArriveTime = Mathf.Infinity;
  11.     float susTime = 3f;
  12.    public float waypointTollerance = 1f;
  13.    public float dwellTime = 3f;
  14.  
  15.     int currentwaypointIndex = 0;
  16.  
  17.    public PatrolPath patrolPath;
  18.     Health health;
  19.     Fighter fighter;
  20.     Mover mover;
  21.  
  22.     public GameObject player;
  23.  
  24.     Vector3 guardPos;
  25.  
  26.    
  27.  
  28.     private void Start()
  29.     {
  30.         instance = this;
  31.         health = GetComponent<Health>();
  32.         fighter = GetComponent<Fighter>();
  33.         player = GameObject.FindWithTag("Player");
  34.         mover = GetComponent<Mover>();
  35.         guardPos = transform.position;
  36.     }
  37.  
  38.  
  39.     private void Update()
  40.     {
  41.         if (health.isDead) return;
  42.         if (InAttackRange())
  43.         {
  44.            
  45.             AttackBehavior();
  46.         }
  47.         else if (timeSinceLastSawPlayer < susTime)
  48.         {
  49.             SusBehavior();
  50.         }
  51.         else
  52.         {
  53.             PatrolBehavior();
  54.         }
  55.         UpdateTimers();
  56.     }
  57.  
  58.     private void UpdateTimers()
  59.     {
  60.         timeSinceLastSawPlayer += Time.deltaTime;
  61.         waypointArriveTime += Time.deltaTime;
  62.     }
  63.  
  64.     private void PatrolBehavior()
  65.     {
  66.         Vector3 nextPos = guardPos;
  67.  
  68.         if(patrolPath != null)
  69.         {
  70.             if(AtWaypoint())
  71.             {
  72.                 waypointArriveTime = 0;
  73.                 CycleWaypoint();
  74.             }
  75.             nextPos = GetCurrentWaypoint();
  76.         }
  77.         if(waypointArriveTime > dwellTime)
  78.         {
  79.             mover.StartMoveAction(nextPos);
  80.         }
  81.        
  82.     }
  83.  
  84.     private void AttackBehavior()
  85.     {
  86.         timeSinceLastSawPlayer = 0;
  87.         fighter.Attack(player);
  88.     }
  89.  
  90.     private void SusBehavior()
  91.     {
  92.  
  93.     }
  94.  
  95.     private bool AtWaypoint()
  96.     {
  97.         float disToWaypoint = Vector3.Distance(transform.position, GetCurrentWaypoint());
  98.         return disToWaypoint < waypointTollerance;
  99.     }
  100.  
  101.     private Vector3 GetCurrentWaypoint()
  102.     {
  103.         return patrolPath.GetWayPoint(currentwaypointIndex);
  104.     }
  105.  
  106.     private void CycleWaypoint()
  107.     {
  108.         currentwaypointIndex = patrolPath.GetNextIndex(currentwaypointIndex);
  109.     }
  110.     private bool InAttackRange()
  111.     {
  112.         float distoPlayer = Vector3.Distance(player.transform.position, transform.position);
  113.         return distoPlayer < chaseDis;
  114.     }
  115.  
  116.     private void OnDrawGizmosSelected()
  117.     {
  118.         Gizmos.color = Color.red;
  119.         Gizmos.DrawWireSphere(transform.position, chaseDis);
  120.     }
  121.  
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement