Add and Remove objects above and below screen

Hey all,

I’m making a 2d game using unity. I’m having a problem with a few formulas.

What I’m doing is creating a blank game object and adding a few other game objects (Square Planes scale of 1) to it. What I’m trying to do is move the container object to just above the screen when its created and populate with the squares.

so like this,
XXXXX
XXXXX
---------- ← Top of screen

It was working when I make a row of these squares by using the ScreenToWorld method of the camera using the Square planes scale in the equation but when I try to add 2 rows of blocks to the game object and use the same formula it gets weird.

I thought maybe I needed to use the main game objects height instead but that is 1 which doesn’t change the formula. I thought maybe there was a would scale or something.

Because I then move the objects down and I need to know when the object is completely on the screen to trigger a a method.

So essentially I need to know how to work out the following 3 scenarios with an object that could be any height in the y direction

  1. Place object above screen
    XXXXX
    XXXXX
    ---------- ← Top of screen

----------- ← Bottom of creen

  1. Work out then the top of the object is at the top of the screen
    ---------- ← Top of screen
    XXXXX
    XXXXX

----------- ← Bottom of screen

  1. Work out when the top of the object is just off the screen
    ---------- ← Top of screen

---------- ← Bottom of screen
XXXXX
XXXXX

Hi, welcome to the forum! (And I hope your name doesn’t say anything about your future behaviour here :wink: )

The magic functions here are Camera.ViewportToWorldPoint and WorldToViewportPoint. The camera’s viewport space corresponds to what is visible on the screen, with the <0,0> coordinate being bottom left and <1,1> being top right. The Z coordinate in viewport space is the distance of the distance of the point from the viewport plane (ie, not the camera position). If the exact position of your objects is not critical then you can hide them when their Y viewport coordinates go slightly below 0 or slightly above 1 (just tweak this amount until the objects are just offscreen when they are hidden).

For more precision, you will want to know the height of the viewport at the distance of the objects. You can get this using the following calculation:-

var vpHeight = 2.0 * distance * Mathf.Tan(Camera.main.fieldOfView * 0.5 * Mathf.Deg2Rad);

Basically, just multiply this value by the viewport Y coordinate to get the height in world units from the bottom of the viewport at the given distance. To get the width of the viewport, multiply the height by the camera’s aspect ratio.

Hey andeeee, thanks for the welcome.

I appreciate the reply. All very helpful. Managed to get it working nicely with the info. Was just having trouble working on a 2d game in a 3d environment :slight_smile:

Thanks heaps.

PS: Don’t worry about the nick name its just to make me look tough and outlawish.

hi folks …!!

is there any way to move the object to the screen top of the camera view. As like Head’s up display. !!
i am creating the scrolling text in plane, i need the place the plane in the screen top. !!
please help me to solve this issue…!!

thanks in advance !!

Arun