How to get closest point off screen?

Hi, in Unity 2D, I would like to move various objects in and out of the screen.

To do this, I need to create a function that gets the nearest position to an object where it appears off screen, while taking sprite bounds into account.

How can I do this?

If you can leave a reply, I will be most grateful

I’m not at all familiar with Unity2D so I don’t know if it happens to use different world units that work with pixels, or screen coordinates, or normalized screen space but hopefully this will get you started.

So the great thing about this problem is that you are working with a shape that’s a two-axis parallelogram and is also axis aligned. So really you can break it down into the process of checking both axes to see which side is closer and then compare those two results to see which axis is closer.

In other words, for the horizontal axis, is the point closer to the left or the right? Then for the vertical is it closer to the top or the bottom? Once you have those two answers then you need to determine which of them is the smaller, the vertical or the horizontal? Then you can simply move your sprite in that direction plus half of either their height or width (based on if you moved vertically or horizontally) to move them off screen.

You’ll need to be more specific on what you’re doing if you need specific answers to what physics queries to use. I’m assuming you’re asking about physics and colliders because you tagged it as such.