Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class towerController : MonoBehaviour
- {
- [SerializeField] private Transform target, _transform;
- public GameObject bullet;
- private float timeToShot;
- public float radius, timeToShotStandart;
- void Start()
- {
- _transform = GetComponent<Transform>();
- }
- void Update()
- {
- timeToShot -= Time.deltaTime;
- if (target != null)
- {
- Debug.Log("Target != null. " + Vector3.Distance(target.position, _transform.position)+" and "+ radius);
- if (Vector3.Distance(target.position, _transform.position) > radius)
- {
- target = null;
- Debug.Log("Distance > " + radius);
- return;
- }
- if ( timeToShot < 0)
- {
- timeToShot = timeToShotStandart;
- Debug.Log("Shot");
- }
- }
- else selectedTarget();
- }
- void OnDrawGizmos()
- {
- Gizmos.color = Color.blue;
- Gizmos.DrawWireSphere(_transform.position, radius);
- if (target!=null)Gizmos.DrawWireSphere(target.position, 1f);
- }
- void selectedTarget()
- {
- Collider2D[] colliders = Physics2D.OverlapCircleAll(_transform.position, radius);
- float minDistance = int.MaxValue;
- foreach (Collider2D collider in colliders)
- {
- if (collider.GetComponent<enemyControl>() != null)
- {
- Debug.Log(collider.name);
- if (Vector3.Distance(collider.GetComponent<Transform>().position, _transform.position) < minDistance)
- {
- target = collider.GetComponent<Transform>();
- minDistance = Vector3.Distance(collider.GetComponent<Transform>().position, _transform.position);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment