Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Ignoring Collider With Raycast 2D

Discussion in '2D' started by Gonzasmpl, Feb 27, 2019.

  1. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Hi, I need to shot a ray from the enemy to the player and it should ignore all the enemies(and prefab bullets) it touches. Though it should detect things like ground or walls to know if the player is "visible" from the perspective of the enemy who shot the ray. E.g.:
    EnemyWhoShotTheRay spotted the player:
    Player <------- Enemy ------- EnemyWhoShotTheRay

    EnemyWhoShotTheRay did not spotted the player:
    Player _____ Wall <-------- Enemy -------- EnemyWhoShotTheRay

    I'm not used to using raycast so I'm sure some things could be improved. Here's my code:
    Code (CSharp):
    1. private List<Transform> inDetectionRange = new List<Transform>();
    2.  
    3. private IEnumerator CheckDetectionRange()
    4.     {
    5.         while(inDetectionRange.Count != 0)
    6.         {
    7.             for (int i = 0; i < inDetectionRange.Count; i++)
    8.             {
    9.                 RaycastHit2D hit = Physics2D.Raycast(transform.position, inDetectionRange[i].position - transform.position, maxDistance, 11);
    10.                 Debug.DrawRay(transform.position, inDetectionRange[i].position - transform.position, Color.red);
    11.                 Debug.Log(hit.collider.name);
    12.             }
    13.             yield return new WaitForSeconds(1);
    14.         }
    15.         hasStartedDetection = false;
    16.         ...
    17.         ...
    18.     }
    I think the issue is that I'm setting the layers wrong, what I understood is that I should put the int of the layer which I want the ray be part of, something like assigning the raycast layer(so that i assigned the ray to layer 11, which ignore collision with enemies and bullets). And I think the problem is with layers because while running it return this error: NullReferenceException: Object reference not set to an instance of an object, referencing the Debug.Log(hit.collider.name), so the ray wouldn't be hitting anything by what I understand.

    If I don't set any layer it works (having unchecked the Queries Start In Colliders box), but the ray collides with the first thing it touches, being or not an enemy.

    Any suggestion is appreciated, thanks!
     
  2. drcrck

    drcrck

    Joined:
    May 23, 2017
    Posts:
    312
    Layer mask in Physics2D.Raycast must be 1 << 11 if you mean "hit only layer 11"
     
  3. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    132
    Exactly instead of (..., maxDistance, 11) write (..., maxDistance, 1 << 11), it is because layer mask is not a layer number it is a Bitmasks.

    or you can get layer mask with multiple of your layer names like this
    LayerMask.GetMask("UserLayerA", "UserLayerB")
     
    Last edited: Mar 1, 2019
  4. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Thank you! I did not know that they work that way. I chose layer 11 because it ignores collisions with enemies and bullets but instead, I should use 1<<8 | 1<<9 which are the layers assigned to player and scenario respectively so that I'm sure the ray only collides with the player and all the objects that belong to the scenario. Thanks again, and have a good day!
     
unityunity