Search Unity

Why RayCast doesn't works?

Discussion in 'Scripting' started by Pavelyev, Mar 25, 2015.

  1. Pavelyev

    Pavelyev

    Joined:
    Mar 25, 2015
    Posts:
    4
    Hello!

    I'm looking survival shooter example. There is a RayCast call is second lesson:
    Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)​
    Trouble is that I have never gain a true on this call. But when I remove floorMask argument it works.
    I have found this overload in documentation:
    public static function Raycast(ray: Ray, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;​
    I can't find in documentation what is DefaultRaycastLayers. I checked floorMask before function call - it has value of 256. It seems this value does the same effect as 0.
    I also read this article http://docs.unity3d.com/Manual/Layers.html and understand that this is a 8 bit mask. But I don't understand why it's only 8 bit, not 16 for example. Is this a number of predefined layers in Unity?
    And why floorMask after
    floorMask = LayerMask.GetMask("Floor");​
    takes a value of 256? I have a layer with name "Floor" and with MeshCollider on it. All right as in tutorial.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The mask should be thought of as a binary. It's a 32-bit field, by the way, not 8. (I assume you got 8 from the 1 << 8 part in that link, but that's for LAYER 8, not a reference to the number of bits in the mask. There is, in fact, 1 bit for each layer, which is exactly how it works - 1 means "it hits this layer", 0 means "it doesn't hit this layer". The actual integer value of the mask is pretty meaningless in and of itself, it's simply the most convenient/efficient representation of the binary data.

    So your floorMask with a value of 256 looks like this:
    Code (csharp):
    1.  
    2.                          X          
    3. 00000000 00000000 00000001 00000000
    4.  
    That X represents your layer 8 spot (counting from the far right, beginning with index 0, and index 31 all the way on the left). You can see how your 256 and your 0 could give almost the same results: they're only different on that one particular layer.

    I believe DefaultRaycastLayers is all the layers except the one named IgnoreRaycast, or this:
    Code (csharp):
    1.  
    2. 11111111 11111111 11111111 11111011
    3.  
    Does this help clear it up?
     
  3. Pavelyev

    Pavelyev

    Joined:
    Mar 25, 2015
    Posts:
    4
    Thank you for explains! I have undestand this.

    By the way my project didn't work because of I didn't assign a "Floor" layer to my Floor game object.