Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycasting colliding with box collider on a the wrong layer.

Discussion in 'Scripting' started by LordBlackwood, Jun 30, 2015.

  1. LordBlackwood

    LordBlackwood

    Joined:
    Aug 10, 2013
    Posts:
    26
    I've searched over a dozen topics already posted on this subject and none of them have worked out. I have a drag and drop system in place based on a system of invisible quads forming a grid. I raycast against these grid tiles as I drag so that my unit follows the cursor. I have a layer mask that to the best of my understanding should mean that the ray being cast should ignore all other layers except for the one with my invisible tiles.

    And yet, for some reason my raycast is colliding with every other "is Trigger" box collider in the scene. Most annoyingly it colliding with my units. I can drag my unit just fine until I bump into another unit. Why oh god, why?

    I have settings set so that the raycast does collide with triggers. I want it this way because all my box colliders are triggers. I have no use for physics. I don't want to put them on the ignore raycast layer because I was OTHER raycasts to collide with them for bullets and such. I just want this one particular raycast to only hit my ground layer, why is this so difficult?

    Here's my code. My ground tiles are on the ground layer and are tagged grid. Nothing else is on the Ground layer, only on layers label Default, Enemy, and Unit. Where have I done goofed? The places to look are my layermark groundLayer, and my functions DragAndSnap() and SpawnObject().

    I really appreciate the help.

    Code (CSharp):
    1.  
    2.     public void SpawnObject()
    3.     {
    4.         //Seems like a good idea to reset our dragging node
    5.         DraggingNode = null;
    6.  
    7.         //fire a ray from camera. pew pew.
    8.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.         if (Physics.Raycast(ray, out hit, groundLayer))
    10.         {
    11.             if (hit.transform.tag == "grid")
    12.             {
    13.                 //if it collides with our grid, spawn our ghost at the collision point
    14.                 specificVector.Set(hit.point.x, hit.collider.transform.position.y, hit.point.z);
    15.                 currentGhost = Instantiate(ghost, specificVector, Quaternion.identity) as GameObject;
    16.  
    17.                 //begin tracking the drag
    18.                 isDragging = true;
    19.             }
    20.         }
    21.     }
    22. }
    23.  
    EDIT: Solved! Removed all irrelevant code to the answer for posterity.
     
    Last edited: Jun 30, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the layer has to be the 4th argument in the raycast, you have made it the 3rd, which is the distance. Since the layermask is technically a number the layermask is usable for the distance, implicitly converting the bit/int/whatever the layermask base datatype is to a float so the raycast doesn't generate an error.

    If you want to use layermasks you need to include a distance parameter, Mathf.Infinity or whatever you like.


    Multiple optional parameters are fun eh? :)
     
    valyntt and LordBlackwood like this.
  3. LordBlackwood

    LordBlackwood

    Joined:
    Aug 10, 2013
    Posts:
    26
    AGH, I'm an idiot. Thank you! That'll teach me for not paying more attention to intellisense. (Or not properly reading the documentation....)

    Thanks!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    of course the only reason I know this is because I did the exact same thing :D