Search Unity

RayCast Mask Problem

Discussion in 'Physics' started by ev3d, Dec 16, 2014.

  1. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    Here is my code:

    Code (CSharp):
    1.  
    2.          Physics.Raycast (ray,out hit,90,mask);
    3.         //Debug.DrawLine (TimingLight.position,direction,Color.blue,9);
    4.         if (hit.transform != null) {
    5.             //Debug.Log (hit.transform.name);
    6.             var LayerHit = hit.transform.gameObject.layer;
    7.            
    8.             if (LayerHit != 12)
    9.             {
    10.                 return LayerHit// <-----Here this is 10?
    11.             }
    12.  
    13.        
    the mask is set with check marks by 2 layers (layer 11 and 12)

    So how is it EVER 10? I tried reversing the mask so everything BUT 11 and 12 are checked but then i get layers like 18, etc... what is going on here?
     
  2. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    i just tried this as well: based on examples here (http://unity3d-book.blogspot.com/2014/03/unity3d-raycast-with-layermask-how-do.html):

    Code (CSharp):
    1. var Cones = 1 << 12;// cones is layer 12
    2.  
    3.         var Timing = 1 << 11;//Timing is layer 11 ;
    4.  
    5.         var Mask2 = Cones | Timing;
    6.  
    7.         Vector3 direction = TimingLight.TransformDirection(new Vector3(0,-200,0));
    8.         var ray = new Ray(TimingLight.position, direction);
    9.         RaycastHit hit;
    10.         Physics.Raycast (ray,out hit,90,Mask2);
    11.         //Debug.DrawLine (TimingLight.position,direction,Color.blue,9);
    12.         if (hit.transform != null) {
    13.             //Debug.Log (hit.transform.name);
    14.             var LayerHit = hit.transform.gameObject.layer;
    15.             //Debug.DrawLine(TimingLight.position,hit.point,Color.yellow);
    16.             //if ((ConeMask.value & 1 << LayerHit) == 0)
    17.             if (LayerHit != 12) //11=timing
    18.             {
    19.                 return BeamTrippedState.Front;
    20.             }
    21.  
    22.         }
    Still hitting layer 10...
     
  3. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    I finally figured it out... It was caused buy the fact that my colider was a child of a Rigid body... So change the code to

    Code (CSharp):
    1. hit.collider.gameObject instead of hit.transform
     
  4. A_never_kill

    A_never_kill

    Joined:
    Jan 7, 2014
    Posts:
    81
    go with hit.collider.gameobject