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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with Trigger and Line of Sight

Discussion in 'Editor & General Support' started by iso250, Nov 6, 2015.

  1. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    Hello all.

    The game I'm making has mines in it. In order to set them off, a robot must be within range of it (in contact with the Trigger Circle Collider 2D) and have line of sight to the mine. This prevents a robot from triggering it from the other side of a wall. Below is the code I have used. (Please note, the end result will not be instant destruction, as is shown by the code. I just use that for testing as it makes it perfectly obvious whether the code is working or not!!)

    For some reason, this code just doesn't work. Funnily enough, when I change the tag to "Player", being the tag that the player has, the code works perfectly and I can't figure out why. I have checked that the layer that the "EnemyRobot" objects are on are included in the LOSLayerMask (as is the layer the player is on) but the enemy robots just don't seem to trigger.

    Can anyone please help me?


    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D collision) {
    2.  
    3.         if (collision.gameObject.tag == "EnemyRobot")
    4.         {
    5.             RaycastHit2D MineLOSTest = Physics2D.Linecast(transform.position, collision.gameObject.transform.position, LOSLayerMask);
    6.  
    7.             if (MineLOSTest.collider != null)
    8.             {
    9.                 if (MineLOSTest.collider.tag == "EnemyRobot")
    10.                 {
    11.                     Destroy(collision.gameObject);
    12.                 }
    13.             }
    14.         }
    15.     }
    16.  
    17.  
    18.     void OnTriggerStay2D (Collider2D collision) {
    19.  
    20.         if (collision.gameObject.tag == "EnemyRobot")
    21.         {
    22.             RaycastHit2D MineLOSTest = Physics2D.Linecast(transform.position, collision.gameObject.transform.position, LOSLayerMask);
    23.            
    24.             if (MineLOSTest.collider != null)
    25.             {
    26.                 if (MineLOSTest.collider.tag == "EnemyRobot")
    27.                 {
    28.                     Destroy(collision.gameObject);
    29.                 }
    30.             }
    31.         }
    32.     }
     
  2. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    Oh, if I add this code between lines 21 and 22 it does draw debug lines to all robots in contact with the CircleCollider2D.

    Code (CSharp):
    1. Debug.DrawLine(transform.position, collision.gameObject.transform.position);
     
  3. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    Wow, it's quiet on the forums for help today!!