Hi Guys,
I’m trying to do a raycast to see if I’ve hit an object. I’m using this code here:
public IEnumerator TrainingBinocularControl()
{
GameObject cam = GameObject.Find("BinocularViewCamera");
GameObject finger = GameObject.Find("TheFinger");
LayerMask mask = GetComponent<GridReference>().layerMask;
bool bHit = false;
RaycastHit hit;
while (!bHit)
{
if (Physics.SphereCast(cam.transform.position, 10, cam.transform.forward, out hit, mask))
{
if(hit.transform.gameObject.tag == "Target")
{
bHit = true;
yield return null;
}
else
{
Debug.DrawRay(cam.transform.position, cam.transform.forward);
Debug.Log("TRY ME: " + hit.transform.gameObject);
// get outta here.. nothing to see here
}
}
yield return new WaitForFixedUpdate();
}
The issue is the layerMask is not working correctly.
I have 2 objects: The target, and the terrain. I’ve created two new layers in the layers panel: Target and Terrain. I’ve assigned the terrain to the Terrain layer in the inspector, and the target to the Target layer in the inspector. In the inspector I also change the layerMask and press play. With different results. These are what I get:
layerMask: Default, Target = “TRY ME: Terrain (UnityEngine.GameObject)”
layerMask: Target = “TRY ME: Terrain (UnityEngine.GameObject)”
layerMask: Terrain = “TRY ME: Terrain (UnityEngine.GameObject)”
layerMask: Everything = N/A - Nothing returns?
layerMask: Nothing = Nothing Returns (no duh there)
This is making no sense to me. All I want is for the terrain to be excluded from the Raycast. Can anyone explain?