dxoraxs

Untitled

May 3rd, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class towerController : MonoBehaviour
  6. {
  7.     [SerializeField] private Transform target, _transform;
  8.     public GameObject bullet;
  9.     private float timeToShot;
  10.     public float radius, timeToShotStandart;
  11.     void Start()
  12.     {
  13.         _transform = GetComponent<Transform>();
  14.     }
  15.  
  16.     void Update()
  17.     {
  18.         timeToShot -= Time.deltaTime;
  19.         if (target != null)
  20.         {
  21.             Debug.Log("Target != null. " + Vector3.Distance(target.position, _transform.position)+" and "+ radius);
  22.             if (Vector3.Distance(target.position, _transform.position) > radius)
  23.             {
  24.                 target = null;
  25.                 Debug.Log("Distance > " + radius);
  26.                 return;
  27.             }
  28.             if ( timeToShot < 0)
  29.             {
  30.                 timeToShot = timeToShotStandart;
  31.                 Debug.Log("Shot");
  32.             }
  33.         }
  34.         else selectedTarget();
  35.     }
  36.  
  37.     void OnDrawGizmos()
  38.     {
  39.         Gizmos.color = Color.blue;
  40.  
  41.         Gizmos.DrawWireSphere(_transform.position, radius);
  42.         if (target!=null)Gizmos.DrawWireSphere(target.position, 1f);
  43.     }
  44.     void selectedTarget()
  45.     {
  46.         Collider2D[] colliders = Physics2D.OverlapCircleAll(_transform.position, radius);
  47.         float minDistance = int.MaxValue;
  48.         foreach (Collider2D collider in colliders)
  49.         {
  50.             if (collider.GetComponent<enemyControl>() != null)
  51.             {
  52.                 Debug.Log(collider.name);
  53.                 if (Vector3.Distance(collider.GetComponent<Transform>().position, _transform.position) < minDistance)
  54.                 {
  55.                     target = collider.GetComponent<Transform>();
  56.                     minDistance = Vector3.Distance(collider.GetComponent<Transform>().position, _transform.position);
  57.                 }
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment