- Home /
Raycast doesnt hit ground
So i have the simple scene:
I simply cannot understand why my raycast hit the ground
In my script i have the following:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000, LayerMask.NameToLayer("Terrain")))
{
Debug.Log(hit.point);
}
Debug.Log(hit.transform.name);
However whenever i attempt to cast the ray it returns nothing (meaning that it doesnt seem to hit anything)
Can anyone tell me why this is happening?
Answer by GanemVisk · Dec 15, 2019 at 11:22 PM
You want to use LayerMask.GetMask("Water")
, which returns the appropriate mask. LayerMask.NameToLayer("Water")
returns the integer associated with that Layer. A mask is a 32-bit sequence where each bit represents whether the layer with that number should be considered. So if Terrain is your number 9 layer, NameToLayer("Terrain") returns 9 and GetMask("Terrain") returns 1000000002 ( 1<<9 )
Answer by cgklutts · Dec 16, 2019 at 12:44 AM
I assume you have this code wrapped within a input.getmouse?
Your answer
Follow this Question
Related Questions
What drawback of frequently changing of Game Object's Layer? 0 Answers
Can't Ignore a Layer when raycat 0 Answers
Ignoring an object (using raycast with layers) is not working 1 Answer
Set transform layer 1 Answer
LayerMask for RayCast 1 Answer