How to make object properly spawn offscreen

hello. i have a game where the player is going upwards, and i’d like some objects to be spawned offscreen and appear as the player moves up. for instance, look at the picture below:

in this case, the screen is 1920x1080, the size of most standard phones. the cloud spawns offscreen, and reveals itself when the player moves up.
the issue is, when i spawn it out of bounds (lets say the X point is 300, and the Y point is -100) the cloud stays stationary and doesn’t come in the camera’s view as the player moves up. how can i achieve this?
to clarify, the cloud is stationary

Calculate the current player Y so now you can create an object like Create PowerUp at RandomX, Player.Y-300 where 300 is a distance from the new object to the player you can change that to fit your needs

Other option is just to create the object knowing the height of your screen. if you set your game for instance to 720x1280 so you know the point 0,0 is the top of your game and the width will be 720 then you create an object at (RandomInRange(0,720)), (-yourobjectHeight)
this will create the object at any random between 0 and 720 in X axis.
And for the Y if your object suppose it’s height is 32px then will be created at Y -32 so when is created is not visible on the screen.

1 Like

i’m running into an issue with both of the options you listed. for the first option, the player is moving up constantly as high speeds, so player.Y-300() won’t always guarantee that the object will spawn offscreen. i tested this, and sometimes, the objects will spawn on the screen, which isn’t good.

with the second option, the player is moving up constantly (like i said before), so -32px will be far below the player after some point. picture below:

are there any alternatives?

You have to add some sort of RandomInRange to calculate the x of the new item because the y will always be the same -y where y is equal to some negative number below 0 like y=-45

1 Like

i already have a randominrange for the x position, thankfully. the fact that the negative y coordinate is the same is my problem.

since the character is moving up, y=-45 for example won’t always be above the player. the player can start at y=20, then go up to y=0, then y= -20, then y= -50. at this point, y=-45 is below the player, and not offscreen. i want the spawned item to always be above the player, and offscreen.

Look at this maybe helps the item always comes from the top

1 Like

Are you using multiple layers, and moving the camera on the layer with the player, but not the layer that the clouds are created on?

the clouds and the player are both on the same layer.

Apologies, I misread your earlier posts. You’ll want to create the clouds with a y position of

`Player.Y() - (ScreenHeight()/2 + [height of cloud image])`

This will always place them just above the top of the screen.

2 Likes

thanks for the response. i adjusted the “ScreenHeight()/2” and instead divided by 1.25. worked like a charm–notably, though, if the number was lower then 1.25 (say 1, for example) the cloud wouldn’t spawn at all.

regardless, it worked. thank you!

this example also helped, thanks a bunch

1 Like