Keeping things inside the camera frustum

so I have an object. it can fly around. But I want to keep him inside the camera’s frustum.

For keeping him inside the near and far, that was simply a matter of doing a Distance from camera to my object (let’s call him Icarus), and then making sure Icarus’s is less than far and farther than near.

But how do I handle the rest of the frustum?

Easiest way would probably be to use Camera.main.ViewportToWorldPoint, somehow. Or vice versa: you can check Camera.main.WorldToViewportPoint(myPosition).x < 0, and so on.

O_O

That’s it?

would I also need to add a near and far frustum check? (saying the gameplane happens to parallel with the camera’s forward vector)

slight problem with this. I’m using this:

public void pushObjectBackInFrustum(Transform obj)
{
	Vector3 pos = Camera.main.WorldToViewportPoint(obj.position);
	if(pos.x < 0)
		pos.x = 0;
	if(pos.x > 1)
		pos.x = 1;
	if(pos.y < 0)
		pos.y = 0;
	if(pos.y > 1)
		pos.y = 1;
	obj.position = Camera.main.ViewportToWorldPoint(pos);
}

unfortunately what happens when I have the camera tilted, it pushes it “up” the frustum till it is at the top of the frustum or it pushes it till it’s pushes down out of the frustum.

If you are just trying to follow an object with your camera, there is a script in the standard assets from the top menu Components->Camera-Control->Smooth Look At which will always orient your camera to your object. If you are trying to merely confine your character to a camera’s static/unmoving frustum, then you might consider just adding invisible box colliders along the borders of the frustum, which you can see in any scene view window. Of course, you’ll have to orient the boxes appropriately. If you’re going for a more mathematical code driven solution, you might do a search on the subject of culling, since culling is fundamentally the process of figuring out which items are outside the frustrum.

Hope this helps!