Another option, which is much more reliable in certain situations is to use
Camera.WorldToScreenPoint (position) to determine if a point in the world is in the camera frustum.
Here is some code I wrote for someother purspose to determine if a point is visible in the camera frustrum. I wrote it a long time ago, so don’t judge the code negatively
Ok thx man i will try it out.I would never judge others code I know what mine looks like.
question what is the someposition on line 2? is it a input required or just a name you assigned?
somePosition represents the position you are testing, you are getting the screenspace coordinate of that position and testing to see if it is on the screen with that method. If the resulting value of the WorldToScreenPoint, is coord that is off screen, it means that point in worldspace is not in the camera frustrum.
Yeah, as passerbycmc said, it is just world coordinate.
YOu could just consider the gameobject’s position as the coordinate.
Vector3 somePosition = gameObject.transform.position;
This is the pivot point.
Note that you need to make sure the renderer/mesh is at the pivot point of the gameobject, otherwise the above solution would give you problems. You could have a gameObject at (0,0,0), but the mesh is at (1000,1000,1000), then it would not work.
So, to get more accurate results you should consider using the mesh.bounds.center for the position to test:
The mesh.bounds.center is the world position of the center of the bounding box which encapsulates the mesh.
This is probably more reliable than the gameObject’s transform position.
I also forgot to mention that using Renderer.isVisible can return false if the mesh is in the frustrum but is being culled by the OC system. SO just because an item is in the frustum of the camera, does not mean isVisible will be true.
Also, an item could be behind the camera, but yet isVIsible is true, because it is required to be rendered for shadows. This is why I don’t like using Renderer.isVisible
i am going to try out the isvisible etc route first not having to do more code seems the way to go especially seen as this is going to be running on hundreds of objects.Long as something isvisible when it should be not to concerned about a few been visible when i rather they weren’t.