Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public abstract class Turret : MonoBehaviour
- {
- [SerializeField]
- protected float rot_Speed;
- protected int target_ID;
- [SerializeField]
- protected string target_Tag;
- public GameObject currentTarget;
- public GameObject thisTurret;
- public bool isFiring;
- public List<GameObject> targets = new List<GameObject>();
- //protected List<Vector3> target_Loc = new List<Vector3>();
- public virtual void Init()
- {
- }
- public virtual void Update()
- {
- }
- public virtual void CheckTarget()
- {
- /* if(targets.Count > 0)
- {
- if(currentTarget != null)
- {
- isFiring = true;
- Debug.Log(this.gameObject +" can attack:" + isFiring);
- }
- else if(currentTarget == null)
- {
- Debug.Log(this.gameObject + " can attack:" + isFiring);
- isFiring = false;
- }
- }
- else if(targets.Count == 0)
- {
- currentTarget = null;
- isFiring = false;
- Debug.Log("No target available for:" + this.gameObject);
- } */
- }
- public virtual void UpdateTargets()
- {
- }
- public virtual void TrackTarget()
- {
- if(currentTarget != null)
- {
- Vector2 direction = (Vector2)currentTarget.transform.position - (Vector2)thisTurret.transform.position;
- float angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
- Quaternion rotation = Quaternion.AngleAxis(angle,Vector3.forward);
- thisTurret.transform.rotation = Quaternion.Slerp(transform.rotation,rotation,rot_Speed * Time.deltaTime);
- }
- }
- public virtual void OnTriggerEnter2D(Collider2D col)
- {
- /* if(col.gameObject.CompareTag(target_Tag))
- {
- Debug.Log(col.name +":Entered attack range");
- targets.Add(col.gameObject);
- } */
- if(col.gameObject.CompareTag(target_Tag))
- {
- if(currentTarget == null)
- {
- currentTarget = col.gameObject;
- }
- }
- }
- public virtual void OnTriggerStay2D(Collider2D col)
- {
- if(col.gameObject.CompareTag(target_Tag))
- {
- if(currentTarget == null)
- {
- currentTarget = col.gameObject;
- }
- }
- }
- public virtual void OnTriggerExit2D(Collider2D col)
- {
- if(col.gameObject.CompareTag(target_Tag))
- {
- if(currentTarget != null)
- {
- currentTarget = null;
- }
- }
- /* if(col.gameObject.CompareTag(target_Tag))
- {
- Debug.Log(col.name + ":Exited attack range");
- foreach(GameObject target in targets)
- {
- if(target.name == col.gameObject.name)
- {
- targets.Remove(target);
- if(targets.Count > 0)
- {
- currentTarget = targets[0];
- }
- }
- break;
- }
- } */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement