How to check if a point is seeable by a camera

Hi. I need to know if a point can be seen by a certain camera or not. Now I'm converting the point world coordinates (as Vector3) to camera Viewport coordinates using Camera.WorldToViewportPoint, then I check if x and y values are outside the range [0..1] for both axis, but I suspect there is an easier way. Any ideas?

use Camera.WorldToViewportPoint, check if X and Y are within 0…1 range AND check Z>0

(if Z is not greater than 0 it means the point is behind the camera.)

Your way is actually one of the easier ways actually - the alternative way I can think of is to check if the point lays in the camera's frustum, which is probably more code

@patrik
Hello! I know you already have some answers but incase someone needs some code for this I will provide it. Also, these answers do not take into account the point being behind an object. Here is some tested and ran code that does it.

public Camera camera;
public bool PointInCameraView(Vector3 point) {
        Vector3 viewport = camera.WorldToViewportPoint(point);
        bool inCameraFrustum = Is01(viewport.x) && Is01(viewport.y);
        bool inFrontOfCamera = viewport.z > 0;

        RaycastHit depthCheck;
        bool objectBlockingPoint = false;

        Vector3 directionBetween = point - camera.transform.position;
        directionBetween = directionBetween.normalized;

        float distance = Vector3.Distance(camera.transform.position, point);

        if(Physics.Raycast(camera.transform.position, directionBetween, out depthCheck, distance + 0.05f)) {
            if(depthCheck.point != point) {
                objectBlockingPoint = true;
            }
        }

        return inCameraFrustum && inFrontOfCamera && !objectBlockingPoint;
    }

public bool Is01(float a) {
        return a > 0 && a < 1;
    }

(This thing stings people every time it’s used, haha. Solidarity!).

I think you need to ensure both x and y are within [0-1] ( not outside )

  • if any value is outside 0-1 range then the point is not visible
  • also if z < 0 then I think object is behind camera