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

Question Raycast2D hitting every layer and not my multiple layers in layermask.

Discussion in 'Scripting' started by Dark_Seth, Aug 26, 2022.

  1. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    130
    Hi All

    I am using the following Code. I am Assigning multiples layers in the rayLayermask. like Enemy and Obstacle.

    But my ray keeps hitting Default, Interactable layers object aswell. Any idea why?

    Code (CSharp):
    1.     Ray2D ray;
    2.     RaycastHit2D hitInfo;
    3.     public LayerMask rayLayerMask;
    4. Transform trnsGunTip;
    5.  
    6. ...... Some Other Code .....
    7.  
    8. void Shoot(){
    9.  
    10.         ray.origin =trnsGunTip.position;
    11.         ray.direction = trnsGunTip.right;
    12.  
    13.         hitInfo= Physics2D.Raycast(ray.origin,ray.direction,rayLayerMask.value);
    14.         if (hitInfo)
    15.         {
    16.             Debug.Log(hitInfo.collider.name);
    17.             Debug.DrawLine(ray.origin, hitInfo.point,Color.red,1f);
    18.         }
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    Code (CSharp):
    1. hitInfo= Physics2D.Raycast(ray.origin,ray.direction,rayLayerMask);
    try this one
     
  3. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    130
    Hi. Thanks I did try that. Still the same issue.
     
  4. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    130
    Fixed.

    Code (CSharp):
    1. hitInfo= Physics2D.Raycast(ray.origin,ray.direction, Mathf.Infinity, rayLayerMask);