Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. // Patrol.cs
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. using System.Collections;
  5.  
  6. public enum RobotState
  7. {
  8.     Idle,
  9.     Patrol,
  10.     Chase,
  11.     Attack,
  12. }
  13. public class Patrol : MonoBehaviour
  14. {
  15.     [SerializeField] private GameObject sight;
  16.     public Transform[] points;
  17.     private int destPoint = 0;
  18.     private NavMeshAgent agent;
  19.     public bool hasPlayerLock;
  20.     private Transform player;
  21.     private float switchTimer;
  22.     [SerializeField] private float idleWaitTime = 3.0f;
  23.     [SerializeField] private float attackTime = 1.0f;
  24.     [SerializeField] private float attackDistance = 2.0f;
  25.     public RobotState state;
  26.  
  27.  
  28.     void Start()
  29.     {
  30.         state = RobotState.Idle;
  31.         switchTimer = idleWaitTime;
  32.         player = GameObject.FindGameObjectWithTag( "Player" ).transform;
  33.         agent = GetComponent<NavMeshAgent>();
  34.  
  35.         // Disabling auto-braking allows for continuous movement
  36.         // between points (ie, the agent doesn't slow down as it
  37.         // approaches a destination point).
  38.         agent.autoBraking = true;
  39.  
  40.         GotoNextPoint();
  41.     }
  42.  
  43.  
  44.     void GotoNextPoint()
  45.     {
  46.         // Returns if no points have been set up
  47.         if( points.Length == 0 )
  48.             return;
  49.        
  50.         // Set the agent to go to the currently selected destination.
  51.         agent.destination = points[destPoint].position;
  52.  
  53.         // Choose the next point in the array as the destination,
  54.         // cycling to the start if necessary.
  55.         destPoint = ( destPoint + 1 ) % points.Length;
  56.     }
  57.  
  58.  
  59.     void Update()
  60.     {
  61.         switch( state )
  62.         {
  63.             case RobotState.Idle:
  64.                 switchTimer -= Time.deltaTime;
  65.                 agent.destination = transform.position;
  66.                 break;
  67.             case RobotState.Attack:
  68.                 switchTimer -= Time.deltaTime;
  69.                 break;
  70.             case RobotState.Chase:
  71.                 agent.destination = player.position;
  72.                 break;
  73.             case RobotState.Patrol:
  74.                 if( !agent.pathPending && agent.remainingDistance < 0.5f )
  75.                     GotoNextPoint();
  76.                 break;
  77.             default:
  78.                 break;
  79.         }
  80.  
  81.         DetermineState();
  82.  
  83.         //Debug.Log( state + ", " + switchTimer );
  84.  
  85.     }
  86.  
  87.     private void DetermineState()
  88.     {
  89.  
  90.         hasPlayerLock = sight.GetComponent<CombatantSight>().hasPlayerLock; // if the sight system has the player, so does the turret
  91.         float dist = Vector3.Distance(player.position, transform.position );
  92.  
  93.         switch( state )
  94.         {
  95.             case RobotState.Idle:
  96.                 if( switchTimer < 0 )
  97.                     state = RobotState.Patrol;
  98.                 if( hasPlayerLock )
  99.                     state = RobotState.Chase;
  100.                 break;
  101.             case RobotState.Attack:
  102.                 if( switchTimer < 0 )
  103.                     Attack();
  104.                 break;
  105.             case RobotState.Chase:
  106.                 if( !hasPlayerLock )
  107.                 {
  108.                     state = RobotState.Idle;
  109.                     switchTimer = idleWaitTime;
  110.                 }
  111.  
  112.                 if( dist < attackDistance )
  113.                 {
  114.                     state = RobotState.Attack;
  115.                     switchTimer = attackTime;
  116.                 }
  117.  
  118.                 break;
  119.             case RobotState.Patrol:
  120.                 if( hasPlayerLock )
  121.                     state = RobotState.Chase;
  122.                 break;
  123.             default:
  124.                 break;
  125.         }
  126.     }
  127.  
  128.     private void Attack()
  129.     {
  130.         // Do Attack Stuff Here
  131.         state = RobotState.Idle;
  132.         switchTimer = idleWaitTime;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement