Search Unity

How to arrange only 'Mesh Collider' to be hit by Raycast?

Discussion in 'Physics' started by filibis, Aug 1, 2017.

  1. filibis

    filibis

    Joined:
    Apr 3, 2017
    Posts:
    35
    I have two scripts to open/close doors when pressed a key within collider range which works fine. Except the UI text (that prints 'E' on screen when looked at door) detects Box Collider (Is Trigger) as well as Mesh Collider which causes text to appear surroundings of the door.
    So, how can i specifically arrange only 'Mesh Collider' (door itself) to be hit by Raycast (for text) and not Box Collider?

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         Ray ray = Camera.main.ViewportPointToRay(new Vector3 (0.5F, 0.5F, 0F));
    4.  
    5.         RaycastHit hit;
    6.  
    7.         if (Physics.Raycast(ray, out hit, Reach))
    8.         {
    9.             if (hit.collider.tag == "Door")

    ---
    Or if there is a better and simpler approach to open/close doors, i would like to hear.
     
  2. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    Simple fix. If you want the door to respond to a walking player/character you could just make the box collider a foot or so high which will still get triggered by a walking players lower part of their capsule collider leaving the raycast clear to look at the upper section of the door.
     
  3. filibis

    filibis

    Joined:
    Apr 3, 2017
    Posts:
    35
    I tried such things but i can't really call it a fix. Because text still appears all over the floor when you look down around the door.
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Send a raycast. Make the raycast ignore Triggers. If they raycast hits a object with the OpenDoor component. Show text.
     
    filibis likes this.
  5. filibis

    filibis

    Joined:
    Apr 3, 2017
    Posts:
    35
    Thank you @TwoTen for directing me to a good point about ignoring Triggers. I found out that i can disable Edit>Project Settings>Physics>Untick 'Queries Hit Triggers' which worked fine (hoping that won't cause any problem in the future).

    However when i add QueryTriggerInteraction.Ignore to my script like this:
    Code (CSharp):
    1. if (Physics.Raycast(ray, out hit, Reach, QueryTriggerInteraction.Ignore))
    2.         {
    3.             if (hit.collider.tag == "Door")
    I get these two errors:
    Any idea what am i doing wrong?
     
  6. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
  7. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You are not using a valid overload.
     
  8. filibis

    filibis

    Joined:
    Apr 3, 2017
    Posts:
    35
    Thanks, that was the issue. I added "1" to the script like this and it works:
    Code (CSharp):
    1. Physics.Raycast(ray, out hit, Reach, 1, QueryTriggerInteraction.Ignore)
    I have another question though; i'm assuming this "1" simply means "Layer 1", am i right? But i realized only odd numbers works and layer context doesn't make sense. That looks strange, could you please explain this to me?
    ---
    I spend a lot of time in the documents, yet i sometimes look blankly at script references o_O since i don't have any coding background and i'm kinda new to this. Thank you for the patience (=
     
  9. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    I honestly don't know how to define bitmasks by code. Never had the need to learn haha.
    I just use public LayerMask properies and let unity serialize it.
     
  10. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    Layermasks are bitmasks. Each bit 'represents' a layer.
    So, if you want to check casts on layer 10 & 12 for instance, you do :

    Code (CSharp):
    1. int layerMask1 = 1 << 10;
    2. int layerMask2 = 1 << 12;
    3. int CombinedMask = layerMask1 | layerMask2;
     
    howong and filibis like this.
  11. howong

    howong

    Joined:
    Oct 9, 2017
    Posts:
    6
    I usually use this:
    raycastLayers = (LayerMask.GetMask (new string[]{ "Default", "Player", "NPC" }));
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    There is an overload for raycasts that lets you pick and choose for future reference.