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

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?

void Update ()
    {
        Ray ray = Camera.main.ViewportPointToRay(new Vector3 (0.5F, 0.5F, 0F));

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Reach))
        {
            if (hit.collider.tag == "Door")

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

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.

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.

Send a raycast. Make the raycast ignore Triggers. If they raycast hits a object with the OpenDoor component. Show text.

1 Like

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:

if (Physics.Raycast(ray, out hit, Reach, QueryTriggerInteraction.Ignore))
        {
            if (hit.collider.tag == "Door")

I get these two errors:

Any idea what am i doing wrong?

I think you are missing the layerMask argument. Check the docs:

You are not using a valid overload.

Thanks, that was the issue. I added “1” to the script like this and it works:

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 :eyes: since i don’t have any coding background and i’m kinda new to this. Thank you for the patience (=

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.

Layermasks are bitmasks. Each bit ‘represents’ a layer.
So, if you want to check casts on layer 10 & 12 for instance, you do :

int layerMask1 = 1 << 10;
int layerMask2 = 1 << 12;
int CombinedMask = layerMask1 | layerMask2;
2 Likes

I usually use this:
raycastLayers = (LayerMask.GetMask (new string[ ]{ “Default”, “Player”, “NPC” }));

There is an overload for raycasts that lets you pick and choose for future reference.