Advertisement
Placido_GDD

Interceptor_Behaviour

Oct 6th, 2021 (edited)
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DroneMove : MonoBehaviour
  6. {
  7.     public Transform target; //current target. Pass a transform here via different script
  8.     public GameObject carrier; //the carrier this object returns to.
  9.     public float moveSpeed;
  10.     public float rotSpeed;
  11.     public float distToTarget;
  12.     public float acqRange;
  13.     public Vector2[] rndPointList;
  14.     public int pointID;
  15.     public bool newTarget; //set by a different script
  16.     public string obj_Tag;
  17.     //
  18.     DroneManager droneManager;
  19.     //
  20.     void Start()
  21.     {
  22.         pointID = 0;
  23.         GenerateRndPoints();
  24.     }
  25.     public void GenerateRndPoints()
  26.     {
  27.        
  28.         while (pointID < rndPointList.Length)
  29.         {
  30.             if (pointID < 1)
  31.             {
  32.                 rndPointList[pointID] = target.position;
  33.             }
  34.             else if (pointID >= 1)
  35.             {
  36.                 rndPointList[pointID].x = target.position.x + Random.Range(-2.5f, 2.5f);
  37.                 rndPointList[pointID].y = target.position.y + Random.Range(-2.5f, 2.5f);
  38.             }
  39.             pointID++;
  40.         }
  41.         pointID = 0;
  42.     }
  43.     void Update()
  44.     {
  45.         if (target != null)
  46.         {
  47.             Move();
  48.             FaceTarget();
  49.         }
  50.         else if (target == null)
  51.         {
  52.             ReturnToCarrier();
  53.             FaceCarrier();
  54.         }
  55.         DistanceCheck();
  56.         if (newTarget == true)
  57.         {
  58.             GenerateRndPoints();
  59.             newTarget = false;
  60.         }
  61.         if (carrier == null)
  62.         {
  63.             Destroy(this.gameObject);
  64.         }
  65.     }
  66.  
  67.     void ReturnToCarrier()
  68.     {
  69.         if (distToTarget > acqRange)
  70.         {
  71.             this.transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
  72.         }
  73.         else if (distToTarget <= acqRange)
  74.         {
  75.             droneManager = carrier.GetComponent<DroneManager>();
  76.             droneManager.drones.Remove(this.gameObject);
  77.             Destroy(this.gameObject);
  78.         }
  79.     }
  80.     void FaceCarrier()
  81.     {
  82.         Vector2 direction = carrier.transform.position - this.transform.position;
  83.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  84.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  85.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
  86.     }
  87.     void Move()
  88.     {
  89.         if (distToTarget > acqRange)
  90.         {
  91.             this.transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
  92.         }
  93.         if (distToTarget < acqRange)
  94.         {
  95.             if (pointID < rndPointList.Length - 1)
  96.             {
  97.                 pointID++;
  98.                 //Debug.Log("Point ID value:" + pointID);
  99.             }
  100.             else if (pointID >= rndPointList.Length - 1)
  101.             {
  102.                 pointID = 0;
  103.                 GenerateRndPoints();
  104.             }
  105.  
  106.         }
  107.     }
  108.  
  109.     void DistanceCheck()
  110.     {
  111.         if (target != null)
  112.         {
  113.             distToTarget = Vector3.Distance(this.transform.position, rndPointList[pointID]);
  114.         }
  115.         else if (target == null)
  116.         {
  117.             distToTarget = Vector3.Distance(this.transform.position, carrier.transform.position);
  118.         }
  119.  
  120.     }
  121.     void FaceTarget()
  122.     {
  123.         //rotate towards target code
  124.         Vector2 direction = rndPointList[pointID] - (Vector2)transform.position;
  125.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  126.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  127.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
  128.  
  129.  
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement