Hello I’m trying to check if an Object is with in Part of a screen then a InSpace = true
, if it is not in that part of the screen then the InSpace = false
.
I have attempted at it, but it only works with in screen space and it does not work in a specific part of the camera’s screen space.
What do I have to do to make this work?
public Transform target;
public Camera camera;
bool InSpace = false;
private void OnGUI()
{
Vector3 screenPoint = camera.WorldToViewportPoint(target.position);
bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
if (onScreen)
{
InSpace = true;
}
else
{
InSpace = false;
Debug.Log("Object is Off Screen");
}
}