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?
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 )
I assume you have this code wrapped within a input.getmouse?