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

Multidirectional LineCastAll

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

  1. iso250

    iso250

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

    I am making a 2D top down game where the player can set off bombs that transmit a virus to all robots that it has line of sight to. I am trying to use LineCastAll but am not having much success. Thus far I have this. The LOSLayerMask does contain the objects I'm looking for as well as the walls that block line of sight.

    Code (CSharp):
    1.     private GameObject[] EnemyReferences;
    2.     private RaycastHit2D[] hits;
    3.      
    4.     void Start () {
    5.  
    6.         EnemyReferences = GameObject.FindGameObjectsWithTag("EnemyRobot");                      
    7.     }
    8.      
    9.     void FixedUpdate () {
    10.  
    11.         for (int i = 0; i < EnemyReferences.GetLength(0); i++)
    12.         {
    13.             hits = Physics2D.LinecastAll(transform.position, EnemyReferences[i].transform.position, LOSLayerMask);
    14.             if (hits[i].collider.tag == "EnemyRobot")
    15.             {
    16.                 print ("Hit");
    17.             }
    18.         }
    When this code activates I sometimes get the debug line printing Hit, but most of the time I get this.

    IndexOutOfRangeException: Array index is out of range.
    SmallLightExplosion.FixedUpdate () (at Assets/Explosions/SmallLightExplosion.cs:61)

    I'm not sure what I'm doing wrong. Can anyone please offer advice on how to fix this?

    Thank you.
     
  2. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    Nobody knows how to fix this at all? :(
     
  3. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'm going to assume that you're populating the EnemyReferences in Start() but some are being destroyed and the array is not being updated to reflect this?
     
  4. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    At the moment I'm testing the above code without any of the enemies being destroyed. When the error happens there are the same number of enemies as there were when the scene started.
     
  5. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Oh, your problem is likely:

    Code (csharp):
    1. if (hits[i].collider.tag == "EnemyRobot")
    2. {
    3.     print ("Hit");
    4. }
    the variable i has no meaning to hits[]..

    Change that to:

    Code (csharp):
    1. for(int hitIndex = 0; hitIndex < hits.Length; ++hitIndex)
    2. {
    3.     if (hits[hitIndex].collider.tag == "EnemyRobot")
    4.     {
    5.         print ("Hit");
    6.     }
    7. }
    to give:

    Code (CSharp):
    1.  
    2.     private GameObject[] EnemyReferences;
    3.     private RaycastHit2D[] hits;
    4.  
    5.     void Start()
    6.     {
    7.         EnemyReferences = GameObject.FindGameObjectsWithTag("EnemyRobot");
    8.     }
    9.  
    10.     void FixedUpdate()
    11.     {
    12.         for (int i = 0; i < EnemyReferences.GetLength(0); i++)
    13.         {
    14.             hits = Physics2D.LinecastAll(transform.position, EnemyReferences[i].transform.position, LOSLayerMask);
    15.  
    16.             for (int hitIndex = 0; hitIndex < hits.Length; ++hitIndex)
    17.             {
    18.                 if (hits[hitIndex].collider.tag == "EnemyRobot")
    19.                 {
    20.                     print("Hit");
    21.                 }
    22.             }
    23.         }
    24.     }
    25.  
    26.  
     
    Last edited: Nov 18, 2015
    iso250 likes this.
  6. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    Excellent! Thank you very much for your help!
     
    larku likes this.