- Home /
Check if is object in camera field and not covered by other object
Hello,
I konw how to check if object is in camera view but how I can check if that object is cover by something elese? In my case I want to turn on or off some effect when I look at light. At this moment my code is straight forward, I have created interface for lightable object with method
public bool isVisibleByCam(Camera cam) {
if (GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(cam), gameObject.GetComponent<Collider>().bounds)) {
return true;
}
return false;
}
I hope there is better way than checking every object in camera view and comparing distnace between camera.
You could use a physics raycast, from the camera to the target object. They raycast will provide the object HIT. If the raycast hits a DIFFERENT object, you know that different object is between the camera and your target. You might need to do several raycasts (probably to the bounds of your target), if you want to check for partial coverage.
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Your answer
