Physics.Raycast raycasting onto two layers?

Hi,

Below is a snippet of code that im having trouble with, Im raycasting to the level (which is on layer Level) from a layer which is on layer Coin. The layerMask I specify is Level, which I assume means it raycasts to specifically that layer.

But the coin (inwhich this code is being called from) still gets hit when you call the raycast function.

Anyone?

				Ray ray = new Ray(transform.position, Vector3.down);
				RaycastHit hitOut;
				if(Physics.Raycast(ray, out hitOut, shadowMask))
				{
					Debug.Log("LAYER! -- " + hitOut.collider.gameObject.layer + " -- MASK "+ shadowMask.value);
					shadowPlane.active = true;
					shadowPlane.transform.position = hitOut.point;				
					shadowPlane.transform.position += Vector3.up * 0.02f;
				
					//float tVal = (hitOut.distance - shadowPlaneScaleLimits.x) /
				
					float tVal = hitOut.distance / 3.0f;
					
					//shadowPlane.transform.localScale = defaultShadowScale;
				
					float newX = shadowPlane.transform.localScale.x * tVal;
					float newY = shadowPlane.transform.localScale.y;
					float newZ = shadowPlane.transform.localScale.z * tVal;
					
					//shadowPlane.transform.localScale = new Vector3(newX, newY, newZ);

				}

Where’s the code that defines the shadowMask? Maybe you’re not setting it properly.

There isnt code its specified in the inspector. I am setting it to “Coin” only but it manages to raycast into two things.

Anyone?

Bump

If you use an instance of Raycast() with a layerMask parameter, you must also provide a (maximum) distance, otherwise your layerMask will be implicilty converted to a float and interpreted as a distance. So instead of

Physics.Raycast(ray, out hit, mask);

use

Physics.Raycast(ray, out hit, Mathf.Infinity, mask);