I’m having trouble figuring out how to code a few things. I can figure everything out about how to do so except for one thing.
I need to perform a raycast that ignores objects listed in an array. Is there any reliable way to do this?
Alternatively, is there any way to have a raycast ignore all objects from a layer except for one specific one, or ignore objects that either have no renderer or have their renders disabled?
I would use Physics.RaycastAll, then iterate through the objects and check if they fit your criteria (the example code happens to do just what you’re asking). Certainly not very fast, but I think raycasts only filter objects based on their layers, so you might not have another option.
//Select which layers you want your raycast to hit in the inspector
public LayerMask layerMask = -1;
//Can be set to ‘Infinity’ in the inspector, to do that, just type the word in the field
public float range = 50f;
void Update(){
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.forward, out hit, range, layerMask)){
//do stuff here
}
}