Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Building a turret without shooting

Discussion in '2D' started by FriedPollo, Oct 8, 2020.

  1. FriedPollo

    FriedPollo

    Joined:
    Sep 24, 2020
    Posts:
    13
    Hi there, I'm trying to build a turret in a tower defence that kills the enemies but doesn't shoot, so it's a range tower. When I find the enemy then I want to destroy() but it doesn't let me do it.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using UnityEngine;
    4.  
    5. public class Firewall : MonoBehaviour
    6. {
    7.  
    8.  
    9.  
    10.     [Header("Attributes")]
    11.     public float range = 0.6f;
    12.  
    13.     [Header("Unity Setup")]
    14.     public string enemyTag = "Enemy";
    15.     public Transform target;
    16.  
    17.  
    18.     void Start(){
    19.       InvokeRepeating("UpdateTarget", 0f, 0.5f);
    20.     }
    21.  
    22.     private void UpdateTarget()
    23.     {
    24.       GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
    25.       float shortestDistance = Mathf.Infinity;
    26.       GameObject nearestEnemy = null;
    27.  
    28.       foreach(GameObject enemy in enemies)
    29.       {
    30.         float distanceToEnemy = Vector2.Distance(transform.position, enemy.transform.position);
    31.         if(distanceToEnemy < shortestDistance)
    32.         {
    33.           shortestDistance = distanceToEnemy;
    34.           nearestEnemy = enemy;
    35.        
    36.         }
    37.       }
    38.  
    39.       if(nearestEnemy != null && shortestDistance <= range)
    40.       {
    41.         target = nearestEnemy.transform;
    42.       } else
    43.       {
    44.         target = null;
    45.       }
    46.  
    47.     }
    48.  
    49.  
    50.     public void Impact()
    51.     {
    52.       Debug.Log("Impact with virus");
    53.       Destroy(target.gameObject);
    54.     }
    55.  
    56.     void OnDrawGizmosSelected()
    57.     {
    58.       Gizmos.color = Color.red;
    59.       Gizmos.DrawWireSphere(transform.position, range);
    60.  
    61.     }
    62.  
    63.   }
    64.  
    65.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    When is
    Impact()
    called? It looks like your logic for finding the nearest enemy is working fine, but I don't see when that nearest enemy is supposed to be destroyed.
     
  3. FriedPollo

    FriedPollo

    Joined:
    Sep 24, 2020
    Posts:
    13
    Sorry I uploaded the wrong one, When I add impact() here, the it kills only the first one
    Code (CSharp):
    1. if(nearestEnemy != null && shortestDistance <= range)
    2.       {
    3.         target = nearestEnemy.transform;
    4.         Impact();
    5.       } else
    6.       {
    7.         target = null;
    8.       }