Two LayerMasks, Only One Registers??

Hi,
I am sorry for posting, but I have an issue with a simple raycast.

I am checking a cast off of two layer masks

        if(Input.GetMouseButtonUp(0)){
              Ray ray = Cam_Main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y - cam_trns.position.y, -cam_trns.position.z));
              Vector3 dir = new Vector3(ray.direction.x * 100, ray.direction.y * 100, ray.direction.z * 100);
            RaycastHit hit;
            if(Physics.Raycast(ray.origin, dir, out hit, Mathf.Infinity, playerLayerMask)){                
                print("playerHit");
                return;
              }     
            if(Physics.Raycast(ray.origin, dir, out hit, Mathf.Infinity, gridLayerMask)){
                print("floor hit");
                return;
              }
        }

The first works fine, the second never registers. The layer masks and layers are set correctly. The collider of the second is set correctly. I am drawing a ray to check the cast, it appears to pass thru said collider.

Any ideas??

You have returns in your if statements. If the first one is true then it hits the return and never checks the second one.

I would also combine the masks into a single mask, do one raycast, and check the layer of the thing that got hit instead of doing two raycasts.

You are multiplying dir with 100. The documentation doesn’t mention the requirement of the direction to be normalised but I bet it has to be. You can put the 100 to the argument list of Physics.Raycast() instead of Mathf.Infinity and it still should do what you want. (But I might be wrong, it’s just an idea.)

It doesn’t. An un-normalized vector can still represent a direction. I’m not saying it’ll work the way you expect, but it will work :slight_smile:

Thanks, but that does nothing.

??

I placed the layerMask that was working and included the the other layer. I am also just using ray.direction. It still works when I hit the player, not the floor tho. Logic would suggest it is hitting another collider first?

EDIT
This is so weird. I duplicated my player, and swapped his layer to the floor and it hits.

???

Ray ray = Cam_Main.ScreenPointToRay(new Vector3(
    Input.mousePosition.x,
    Input.mousePosition.y - cam_trns.position.y, // Why are you doing this?
    -cam_trns.position.z) // And why are you doing this?

Are you really hitting the object in question?

Edit: You might want to use Unity.DrawRay to be sure.

I was using that as a way to offset cam pos. Turns out, it seems that I dont have to.

I am not sure what you mean. Am I hitting the object in question.

Here is pic. It shows the floor which is not resullting in a return, despite being on the correct layer. It shows two ZomBunnies (players), however one is set to floor layer and it returns as if it were a floor. I then set the floor to the player layer, thinking maybe that would return but no.

For some reason, my floor, set to the floor layer (gridLayerMask in example) will not return. It is a cube, primitive, stretched out. Fully in view of cam and according to the drawn ray, hitting. But not registering.

??

By asking if you are really hitting your object, I wanted to know if the ray you are creating is correctly positioned. If you are able to hit the bunnies, the ray seems to be (somewhat) correct. I’ve noticed another thing now. The “isTrigger” checkbox of your floor seems to be active. What happens if you disable it?

Oh my god.
It works!!!

Crazy, I did not realize a trigger did not fire. I thought the bunnies were triggers.

ok.
THANKS!!!

You’re welcome! :slight_smile:

1 Like