- Home /
view bounds perspective camera
How would I stop a rigid body leaving the camera's view. I want to stop it from leaving the view bounds. So in other words if it goes to the edge of the camera's view it will stop (bounce off)...
Ive looked everywhere but cant find a solution
Answer by Xarbrough · Sep 20, 2015 at 12:39 PM
You can get the position of your object and convert it from world to viewport space. The latter is normalized between 0 and 1, which means you can simple check when its value is below 0 or above 1 to see when it leaves the screen.
When talking about a rigidbody, it sounds like you're using Physics, which might not be the ideal choice if you want continuous motion etc, like in a game of Breakout or Pong. For these types of games, you can simply use the Transform component and move it. But if you really need Physics, you can still create a collider which matches the size of your screen by using ViewportToWorldPoint. To make things easy, you could just send in the four corner viewport coordinates and provide a z-value to place it in space. Then you can scale your box collider to fit in between. Or, of course, eyeball it by setting the values in the inspector by hand, if you know that you're not going to change the screen size much.
im trying to make a dice game, where you click and drag the dice to get it going and it rolls onto a side (etc) thus the reason for the physics