- Home /
GameObject and Camera visibility
Hi,
I'm trying to write a script that allows you to interact with a gameobject, but only when it's in the main camera view.
I've tried Render.IsVisible and I'm trying void OnBecomeVisible but it keeps behaving as if they are all in the camera view when they aren't
Here is the code attached to the GameObject I want to move (to the left) on a key press but only when it's in the camera view.
public float leftSwipe;
public static bool isSeen;
// Use this for initialization
void Start () {
isSeen = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space) && isSeen==true)
{
transform.position = new Vector3(-leftSwipe, transform.position.y, transform.position.z);
}
}
private void OnBecameVisible()
{
isSeen = true;
}
private void OnBecameInvisible()
{
isSeen = false;
}
I'm not sure if I've completely misunderstood how the above functions work. Thanks for any help
Render.IsVisible didnt work? it should work. THe object have a mesh renderer i supose
Answer by phosphoer · Feb 14, 2020 at 06:05 PM
OnBecameVisible()
and IsVisible
should work, however it may not be the exact behavior you're looking for depending on your setup, as it will include times when the object is rendered due to just its shadow being in-frame. It also will be affected by all cameras in the scene, including the scene-view camera when running in editor. I'd guess that if you aren't receiving these messages at all, your script may not be on the same object as the Renderer (The message does not propagate up the hierarchy).
Another approach you could use would be to just calculate whether the object's position is within the frustum of the camera. It's quite simple to test a single point for this, though if your object is large or oblong you may run into issues where a part of the object is visible on camera but the point you are testing is not. But for simple interaction tests this may be good enough! You could just pass Camera.main
to this function with your objects position.
public static bool IsWorldPositionInViewport(Vector3 worldPos, Camera cam)
{
Vector3 viewPosition = cam.WorldToViewportPoint(worldPos);
return viewPosition.z > 0 && viewPosition.x > 0 && viewPosition.y > 0 && viewPosition.x < 1 && viewPosition.y < 1;
}
Thanks for the fast reply. It was the scene view camera, I had no idea it worked like that so thank you! Is there any way to make it so the object can only be interacted with once it's fully on the screen. Like you said, the shadow seems to be causing it to be classed as in view.
Unfortunately I don't know of a built-in method that specifically tests whether a renderer's geometry is within the viewport. As far as I know Unity only concerns itself with whether the renderer needs to be drawn, which will include cases that you probably want to exclude.
Another thing to consider, do you care about occlusion? e.g., if your interactable is behind a door, but the camera is looking towards it, do you need to handle that?
Either way, I think my recommendation would still be to use the code I shared and test whether the object is in the viewport manually. You could expand this to test multiple points if your object is oddly shaped and you want to support it being partially off-screen.
If occlusion matters to you, then you could additionally perform a raycast from the camera to the point that was in the viewport and see if it hit anything in-between.