Wednesday, December 1, 2021

Check gameobject is visible on screen

 


Answer by Taylor-Libonati 

Here's the method I used with success:

Convert the position of the object into viewport space. If the viewport space is within 0-1 you are in the cameras frustum. This method also allows you to add a margin amount if you want, so that objects turn on when they are almost in view.

  1. Vector3 screenPoint = playerHead.leftCamera.WorldToViewportPoint(targetPoint.position);
  2. bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;

No comments:

Post a Comment