Placido_GDD

Proj_Tube

Dec 1st, 2021 (edited)
512
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Proj_Tube : TorpedoSystem
  6. {
  7.     public string Keycode;
  8.     protected GuidedProj projScript;
  9.     public GameObject torpedo_Target;
  10.     RaycastHit2D hitObj;
  11.     Vector2 raycastPos;
  12.     public bool FiringAI;
  13.     public bool canFire; //determines if the player can launch a salvo of torpedos
  14.     public float t_Delay; //determines how long between each torpedo launched.
  15.     public float cooldown; //determines how often you can launch torpedos
  16.     public int torpedoAmt; //determines how many torpedos are spawned.
  17.     int torpedoCount; //current number of torpedos spawned.
  18.     // Start is called before the first frame update
  19.     void Start()
  20.     {
  21.         canFire = true;
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     void Update()
  26.     {
  27.         if (Input.GetKeyDown(Keycode) && base.isAI == false)
  28.         {
  29.             if (canFire == true)
  30.             {
  31.                 FiringMethod();
  32.                 StartCoroutine(CDCheck());
  33.             }
  34.            
  35.         }
  36.  
  37.         if (Input.GetMouseButton(0) && base.isAI == false)
  38.         {
  39.             AssignTarget();
  40.             Debug.Log("Locked on:" + torpedo_Target);
  41.         }
  42.  
  43.         AIFiring();
  44.     }
  45.  
  46.     public void AIFiring()
  47.     {
  48.         if (FiringAI == true && base.isAI == true)
  49.         {
  50.             if (canFire == true)
  51.             {
  52.                 FiringMethod();
  53.                 StartCoroutine(CDCheck());
  54.             }
  55.  
  56.         }
  57.     }
  58.     void FiringMethod()
  59.     {
  60.         if (base.isGuided == false)
  61.         {
  62.             StartCoroutine(TorpedoSalvo());
  63.         }
  64.         else if ((base.isGuided == true) && (torpedo_Target != null))
  65.         {
  66.             base.AssignTarget(torpedo_Target);
  67.             StartCoroutine(TorpedoSalvo());
  68.         }
  69.     }
  70.  
  71.    
  72.     IEnumerator TorpedoSalvo()
  73.     {
  74.         torpedoCount = 0;
  75.         while (torpedoCount < torpedoAmt)
  76.         {
  77.             base.LaunchTorpedos();
  78.             torpedoCount++;
  79.             yield return new WaitForSeconds(t_Delay);
  80.         }
  81.     }
  82.     IEnumerator CDCheck() //checks if the player can fire a salvo of torpedos
  83.     {
  84.         canFire = false;
  85.         yield return new WaitForSeconds(cooldown);
  86.         canFire = true;
  87.     }
  88.     void AssignTarget()
  89.     {
  90.         raycastPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  91.         hitObj = Physics2D.Raycast(raycastPos, Vector2.zero);
  92.         if (hitObj.collider != null)
  93.         {
  94.             if (hitObj.collider.gameObject.tag == "Enemy_C")
  95.             {
  96.                 torpedo_Target = hitObj.collider.gameObject;
  97.             }
  98.         }
  99.         else if (hitObj.collider == null)
  100.         {
  101.             torpedo_Target = null;
  102.            
  103.             Debug.Log("Please select a target");
  104.         }
  105.     }
  106. }
  107.  
RAW Paste Data