Placido_GDD

AI_Battlecruiser

Dec 26th, 2021 (edited)
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BattlecruiserAI : BaseAILogic
  6. {
  7.     public ShipHealth ship_Hp;
  8.     public bool isWandering;
  9.     public bool forwardRayHit;
  10.     public float forwardRayAmt;
  11.     public GameObject[] missileRacks;
  12.     Proj_Tube missileRackScript;
  13.     public int missileID;
  14.     public GameObject[] torpedoRacks;
  15.     Proj_Tube torpedoRackScript;
  16.     SpawnT_Tubes spawn_TT;
  17.     public int torpedoID;
  18.     public float aggroDistance;
  19.     public float engageDistance;
  20.     float currDist;
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         ship_Hp = base.shipAI.GetComponent<ShipHealth>();
  25.         spawn_TT = GetComponentInChildren<SpawnT_Tubes>();
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update()
  30.     {
  31.         GetEngageDist();
  32.         FrontalRaycast();
  33.         //Debug.Log("Distance to player:" + currDist);
  34.  
  35.         if (currDist > stoppingDist)
  36.         {
  37.             MoveToDestination(getDistance(Player.transform.position),Player.transform.position);
  38.            
  39.         }
  40.        if (currDist < engageDistance)
  41.         {
  42.             missileID = 0;
  43.             UseMissiles();
  44.         }
  45.        else if (currDist > engageDistance)
  46.         {
  47.             missileID = 0;
  48.             DontUseMissiles();
  49.         }
  50.     }
  51.  
  52.     void GetEngageDist()
  53.     {
  54.         currDist = getDistance(Player.transform.position);
  55.     }
  56.  
  57.     void FrontalRaycast()
  58.     {
  59.  
  60.        Ray ObjectRay = new Ray(transform.position + (transform.right * 2), transform.right * forwardRayAmt);
  61.        Debug.DrawRay(transform.position + (transform.right * 2), transform.right * forwardRayAmt,Color.green);
  62.         RaycastHit2D hit = Physics2D.Raycast(ObjectRay.origin,ObjectRay.direction);
  63.         if (hit.collider != null)
  64.         {
  65.            
  66.             if (hit.collider.tag == "Player_C")
  67.             {
  68.                 Debug.Log("Target in Range:" + hit.collider.name);
  69.                 forwardRayHit = true;              
  70.                 //Debug.Log("Current Distance:" + currDist);
  71.                 while (torpedoID < torpedoRacks.Length)
  72.                 {
  73.                     torpedoRackScript = torpedoRacks[torpedoID].GetComponentInChildren<Proj_Tube>();
  74.                     torpedoRackScript.FiringAI = forwardRayHit;
  75.                     Debug.Log("Torpedo Rack:" + torpedoID + " Firing");
  76.                     torpedoID++;
  77.                 }
  78.             }
  79.         }
  80.         else if (hit.collider == null)
  81.         {
  82.             Debug.Log("No collider detected");
  83.             forwardRayHit = false;
  84.         }
  85.     }
  86.     void UseMissiles()
  87.     {
  88.        while (missileID < missileRacks.Length)
  89.         {
  90.             missileRackScript = missileRacks[missileID].GetComponentInChildren<Proj_Tube>();
  91.             missileRackScript.torpedo_Target = Player;
  92.             missileRackScript.FiringAI = true;
  93.             //Debug.Log("Missile Rack:" + missileID + " Firing.");
  94.             missileID++;
  95.         }
  96.     }
  97.     void DontUseMissiles()
  98.     {
  99.         while (missileID < missileRacks.Length)
  100.         {
  101.             missileRackScript = missileRacks[missileID].GetComponentInChildren<Proj_Tube>();
  102.             missileRackScript.torpedo_Target = null;
  103.             missileRackScript.FiringAI = false;
  104.             missileID++;
  105.         }
  106.     }
Add Comment
Please, Sign In to add comment