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

How can I make the enemies go only to the right or left sides of a tower?

Discussion in 'Scripting' started by trmhtk2_unity, Jan 25, 2022.

  1. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    Hello, I'm trying to create a Tower defense game, and I'm making the enemies go towards the towers and attack them - but I want the enemies to attack it only in the two corners of the tower - like that:
    970311-3fd35107f7c6d138fdb375d9456b957a.png


    , how do I do that?
    Here is the script I use:
    Code (CSharp):
    1. private void LookForTargets() {
    2.         Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, targetMaxRaduis);
    3.         foreach(Collider2D collider in colliders) {
    4.             Building building = collider.GetComponent<Building>();
    5.             if (building != null) {
    6.                 if(targetTransform == null) {
    7.                     targetTransform = building.transform;
    8.                 }
    9.                 else {
    10.                     if(Vector3.Distance(transform.position,building.transform.position) <
    11.                        Vector3.Distance(transform.position, targetTransform.position)) {
    12.                         //Closer!
    13.                         targetTransform = building.transform;
    14.                     }
    15.                 }
    16.             }
    17.         }
    18.    
    19.         if(targetTransform == null) {
    20.             if (BuildingManger.Instance.GetHQBuilding() != null) {
    21.  
    22.                 targetTransform = BuildingManger.Instance.GetHQBuilding().transform;
    23.             }
    24.         }
    25.     }
     
    Last edited: Jan 26, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Your image didn't work. Try uploading it in a fresh message below, using the Unity forum image and inserting the full image.

    Usually if you want discrete points for agents to travel to relative to a single tower, you make blank gameobjects slightly offset away, and use those gameobjects as the destination where the agents travel to.

    If you have multiple agents and need to deconflict them (eg, keep two agents from going to the same spot), then you need some type of destination manager and a way to tell an agent "Sorry, there's nowhere for you to go now."
     
  3. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    Thanks for the explanation, I uploaded the picture through the forum system and now see it