Placido_GDD

StationDroneBehaviour

Dec 13th, 2021 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class StationDroneBehavior : MonoBehaviour
  6. {
  7.     public float moveSpeed;
  8.     public float rotSpeed;
  9.     public float minRndRange;
  10.     public float maxRndRange;
  11.     public float droneAccRange;
  12.     public Vector2[] wayPoints;
  13.     public Vector2[] swarmPoints;
  14.     public Vector2[] targetPoints;
  15.     private Vector2 currentTrgtPos;
  16.     public GameObject targetStation;
  17.     public GameObject target;
  18.     public string droneTarget;
  19.     public int pointID;
  20.     public int LogicID;
  21.     private float distToTargetPoint;
  22.     private bool genAtkPoints;
  23.  
  24.     void Start()
  25.     {
  26.         GenerateRndPoints(targetStation.transform.position,swarmPoints);
  27.     }
  28.  
  29.     void GenerateAtkPoints(GameObject target, Vector2[] targetPoints)
  30.     {
  31.         targetPoints[0] = this.transform.position;
  32.         targetPoints[1] = target.transform.position;
  33.     }
  34.     void GenerateRndPoints(Vector2 targetPos,Vector2 [] waypoint)
  35.     {
  36.         while (pointID < waypoint.Length)
  37.         {
  38.             waypoint[pointID] = new Vector2(targetPos.x + Random.Range(minRndRange,maxRndRange), targetPos.y + Random.Range(minRndRange, maxRndRange));
  39.             pointID++;
  40.         }
  41.         pointID = 0;
  42.     }
  43.  
  44.     void Update()
  45.     {
  46.        
  47.         switch (LogicID)
  48.         {
  49.             case 0:
  50.                 MoveTo(wayPoints[pointID], wayPoints);
  51.                 FaceTarget(wayPoints[pointID]);
  52.                 break;
  53.             case 1:
  54.                 MoveTo(swarmPoints[pointID],swarmPoints);
  55.                 FaceTarget(swarmPoints[pointID]);
  56.                 break;
  57.             case 2:
  58.                 MoveTo(targetPoints[pointID], targetPoints);
  59.                 FaceTarget(targetPoints[pointID]);
  60.                 break;
  61.         }
  62.  
  63.         if((target != null) && (currentTrgtPos != (Vector2)target.transform.position))
  64.         {
  65.             Debug.Log("Chasing Enemy");
  66.             GenerateAtkPoints(target, targetPoints);
  67.             GenerateRndPoints(target.transform.position, swarmPoints);
  68.             currentTrgtPos = target.transform.position;
  69.             LogicID = 2;
  70.             pointID = 0;
  71.         }
  72.     }
  73.  
  74.     void MoveTo(Vector2 targetPos,Vector2[] waypoint)
  75.     {
  76.         DistanceCheck(targetPos);
  77.        
  78.         if (distToTargetPoint > droneAccRange)
  79.         {
  80.             this.transform.Translate(moveSpeed * Time.deltaTime,0,0);
  81.         }
  82.         else if (distToTargetPoint < droneAccRange)
  83.         {
  84.             if (pointID < waypoint.Length - 1)
  85.             {
  86.                 pointID++;
  87.             }
  88.             else if (pointID >= waypoint.Length - 1)
  89.             {
  90.                 switch (LogicID)
  91.                 {
  92.                     case 0:
  93.                         LogicID = 1;
  94.                         pointID = 0;
  95.                         break;
  96.                     case 1:
  97.                         pointID = 0;
  98.                         break;
  99.                     case 2:
  100.                         LogicID = 1;
  101.                         pointID = 0;
  102.                         break;
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     void DistanceCheck(Vector2 targetPos)
  109.     {
  110.         distToTargetPoint = Vector2.Distance(this.transform.position, targetPos);
  111.     }
  112.  
  113.     void FaceTarget(Vector2 targetPos)
  114.     {
  115.         //rotate towards target code
  116.         Vector2 direction = targetPos - (Vector2)transform.position;
  117.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  118.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  119.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
  120.     }
  121.  
  122.     void OnTriggerEnter2D(Collider2D col)
  123.     {
  124.         if(target == null && col.CompareTag(droneTarget))
  125.         {
  126.             Debug.Log("Encountered Enemy");
  127.             target = col.gameObject;
  128.             currentTrgtPos = target.transform.position;
  129.             GenerateAtkPoints(target,targetPoints);
  130.             GenerateRndPoints(target.transform.position, swarmPoints);
  131.             LogicID = 2;
  132.             pointID = 0;
  133.         }
  134.     }
  135.  
  136.     void OnTriggerExit2D(Collider2D col)
  137.     {
  138.         if (target != null && col.CompareTag(droneTarget))
  139.         {
  140.             Debug.Log("Leashed");
  141.             target = null;
  142.             GenerateRndPoints(targetStation.transform.position, swarmPoints);
  143.             LogicID = 0;
  144.             pointID = 1;
  145.         }
  146.     }
  147. }
  148.  
Add Comment
Please, Sign In to add comment