Is there an easy way to constrain a rigidbody to the camera’s view? My character is a rigidbody constrained to the <x, z> axis. Right now I’m projecting the screen coordinate onto a plane at y=0 to determine the four corners of the visible area in 3d space, and then manually restricting the position of the rigidbody to that space. It works, but it struck me as something people might want to do a lot, so I wasn’t sure if there was a faster and more intuitive way of doing it, especially as I’m adding objects that aren’t necessarily bound to a ground-plane.
This sounds dangerous! ![]()
Is there a really good reason you’re not using fixed boundaries, but instead relying on what’s visible to a camera? Even fixed boundaries you create programmatically would make more sense. Constraining a non-kinematic rigidbody in a way that doesn’t make direct use of Bullet or the Physics API doesn’t sound wise, as you’ll have to simulate “real” collision responses mathematically to avoid unrealistic behavior or things like velocity accumulation errors.
As a direct answer, it sounds like you’ve chosen your preferred method of visibility determination already. OnBecameInvisible() and renderer.isVisible don’t seem to apply, nor does accessing the camera’s view frustrum, but those are the first alternatives to your projection method that come to mind.
Yeah I can’t use fixed boundaries because the camera’s view frustum is procedurally variable. I just want to make sure my character can’t leave the visible area when the camera is able to zoom in and out, and rotate. If I had a fixed boundary I’d use colliders to constrain the character. I considered parenting colliders to the camera, but the camera’s FOV changes.
can you not dynamically move the boundary objects based on what the camera can see?
you could work out the four corners of the camera in world space and manipulate the boundary object collider to make the walls expand/shrink on fov change?
I could (and probably should) but that only half solves the problem. Objects above y=0 would still be able to leave the camera’s view. I was hoping there was a method or simple procedure for turning the camera’s view frustum into a rigidbody cage, or perhaps a trick to achieving the same result tht i havent thought of.