Advertisement
Placido_GDD

Turret

Nov 10th, 2021 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public abstract class Turret : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     protected float rot_Speed;
  9.     protected int target_ID;
  10.     [SerializeField]
  11.     protected string target_Tag;
  12.     public GameObject currentTarget;
  13.     public GameObject thisTurret;
  14.     public bool isFiring;
  15.     public List<GameObject> targets = new List<GameObject>();
  16.     //protected List<Vector3> target_Loc = new List<Vector3>();
  17.    
  18.     public virtual void Init()
  19.     {
  20.        
  21.     }
  22.     public virtual void Update()
  23.     {
  24.     }
  25.    
  26.     public virtual void CheckTarget()
  27.     {
  28.         /* if(targets.Count > 0)
  29.         {
  30.                 if(currentTarget != null)
  31.             {
  32.                 isFiring = true;
  33.                 Debug.Log(this.gameObject +" can attack:" + isFiring);
  34.             }
  35.             else if(currentTarget == null)
  36.             {
  37.                 Debug.Log(this.gameObject + " can attack:" + isFiring);
  38.                 isFiring = false;
  39.             }
  40.         }
  41.         else if(targets.Count == 0)
  42.         {
  43.             currentTarget = null;
  44.             isFiring = false;
  45.             Debug.Log("No target available for:" + this.gameObject);
  46.         } */
  47.     }
  48.     public virtual void UpdateTargets()
  49.     {
  50.        
  51.        
  52.     }
  53.    
  54.     public virtual void TrackTarget()
  55.     {
  56.         if(currentTarget != null)
  57.         {
  58.         Vector2 direction = (Vector2)currentTarget.transform.position - (Vector2)thisTurret.transform.position;
  59.         float angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
  60.         Quaternion rotation = Quaternion.AngleAxis(angle,Vector3.forward);
  61.         thisTurret.transform.rotation = Quaternion.Slerp(transform.rotation,rotation,rot_Speed * Time.deltaTime);
  62.         }
  63.            
  64.        
  65.     }
  66.     public virtual void OnTriggerEnter2D(Collider2D col)
  67.     {
  68.         /* if(col.gameObject.CompareTag(target_Tag))
  69.         {
  70.             Debug.Log(col.name +":Entered attack range");
  71.             targets.Add(col.gameObject);
  72.         } */
  73.         if(col.gameObject.CompareTag(target_Tag))
  74.         {
  75.             if(currentTarget == null)
  76.             {
  77.             currentTarget = col.gameObject;
  78.             }
  79.         }
  80.        
  81.     }
  82.    
  83.     public virtual void OnTriggerStay2D(Collider2D col)
  84.     {
  85.         if(col.gameObject.CompareTag(target_Tag))
  86.         {
  87.             if(currentTarget == null)
  88.             {
  89.                 currentTarget = col.gameObject;
  90.             }
  91.  
  92.         }
  93.     }
  94.     public virtual void OnTriggerExit2D(Collider2D col)
  95.     {
  96.         if(col.gameObject.CompareTag(target_Tag))
  97.         {
  98.             if(currentTarget != null)
  99.             {
  100.                 currentTarget = null;
  101.             }
  102.  
  103.         }
  104.         /* if(col.gameObject.CompareTag(target_Tag))
  105.         {
  106.             Debug.Log(col.name + ":Exited attack range");
  107.             foreach(GameObject target in targets)
  108.             {
  109.                 if(target.name == col.gameObject.name)
  110.                 {
  111.                     targets.Remove(target);
  112.                     if(targets.Count > 0)
  113.                     {
  114.                         currentTarget = targets[0];
  115.                     }
  116.                 }
  117.                 break;
  118.             }
  119.         } */
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement