Physics.Linecast does not return collider between start and end.
The problem in question description.
Proff 1:
Proff 1 explanation: We cast a Line from Context (the box that casts lines ) to all points in the scene that satisfy an IF statement provided in the next picture. Obviously, you can see Line passes through Wall(13) that has tag == Enemy and a valid Box Collider. So I made a function, getAllVisiblePositions, where first I get AllPositionsInDirection (explanation in code comments) and then for each point in that list, I cast a line and check if it hit the Wall(13), in the picture, at least 2 lines should hit that wall, and make it impossible to be added to list of visible points. In the console, the TRUE string is not printed, like it never hits the Wall(13).
Number 11 is Layer for the floor, so the line cast ignores it. Component debugObjectValidity is on every point in sceen.
Am I doing something wrong, or this is a bug?
Answer by streeetwalker · Oct 03, 2020 at 05:02 PM
HI @unity_4XWcnhMEUZ043g, chances are the problem is the layer mask. How did you come up with 11 for the layer mask?
If you are testing objects in only one specific layer, the integer representation of the layer mask will always be a power of 2. So, 11 looks suspect.
The layer mask is not the number of the layer - it is a 1 bit-shifted by the number of the layer.
There are a number of ways to generate a layer mask - I always prefer to set up a public LayerMask variable and then choose the layers I need from the menu that displays for that variable in the inspector.
Otherwise, if you only need to check your raycast against one layer, you can use 1 << number of layer
to get the correct layer mask, and either use that directly in your raycast or store it in a variable up front. If you are doing this repeatedly, store it in a variable and use that variable in your linecast call.
Hi, and thanks for the review, I think I can agree with what you said about layers, so I did few changes and instead of linecast i used raycast and this is what are results from the new change:
So i changed the code just to check if my raycasts are all valid, but it seems they are not, for some oddreason, it does not cast ray to all yellow points, that are infront of him (Direction) function argument. So thinks makes a problem that i didnt expect, this makes even another problem, i am not sure how to solve, becose i have array of points, and i cast ray to every single of them, but ingame that does not seem like i did it...
Well, at this point you're going to need to verify the positions of the yellow points in your loop - it seems those do not match the actual yellow points. Are those yellow points actual game objects? $$anonymous$$aybe the mesh is offset from the gameobject center, or something?
Did you verify that you had constructed the Layer$$anonymous$$ask such that the resultant integer representation - 11 - included the Wall layer?
Thanks for your try to solve this problem, I think il just start over, because this simple problem turned into a nightmare kind of... Thanks anyway.
Your answer
![](https://koobas.hobune.stream/wayback/20220613013015im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script function being applied to everything it's attached to logic error 2 Answers
Why does unity not see monobehaviour? 2 Answers
Menu object not responding 0 Answers
Disable array of components 1 Answer