- Home /
How to Check If an Object Is with certain part of the Screen
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");
}
}
You simply have to change the limits 0
and 1
.
bool onScreen = screenPoint.z > 0 && screenPoint.x > 0.4 && screenPoint.x < 0.6 && screenPoint.y > 0.4 && screenPoint.y < 0.6;
@Hellium I know that, but it is based off of the screenSize, I'm just trying to make at a fixed size that is not based off of the screen.
You will have to be more specific.
I'm just trying to make at a fixed size that is not based off of the screen.
But, your question is literally
How to Check If an Object Is with certain part of the Screen
Answer by mlnczk · Nov 20, 2018 at 08:41 AM
Just add colliders and check isTrigger. Add two functions onTriggerEnter() and onTriggerExit() and relay your boolean based on that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182318im_/https://answers.unity.com/themes/thub/images/avi.jpg)