respawn prefab near to end of the screen.

I’m developing a game which has some pipes like in flappy bird. My first approach is to create three pipe objects in the scene and two pipes inside the screen and other one is out of the screen so whenever camera moves forward other objects get into screen. But when camera moves forward I should be able to place pipes randomly and near to the end of the screen.

my idea is to get the end of the screen x axis and deduct some amount and place spawn the pipe object. My question is to how to get the end of x axis in screen. or any good approach would be appreciate. thank you in advance.


for your initial question

there is a screen to world coordinate function.

taking your screen space as being 0,0 at the bottom left and 1,1 as the top right.
if you use cameraViewportToWorldPoint() you can get the actual world space coordinates of a specific area of your screen based on the cameras clipping plane ( ie what you can see in the game view)

taken from the Scripting API guide.

Vector3 p = camera.ViewportToWorldPoint(new Vector3(1, 1, camera.nearClipPlane))

for the example given there, you feed in a vector3 value for your screen, so for the above it is 1,1 which is the top right of your viewable area, the Z value is the clipping plane, the distance from your camera to your viewable game screen area.

so if you were to use the above then p.x (getting the x component of the result of this) would give you the x component of the coordinates to your right hand side of the screen.

Something else to ponder over

theres another option you may consider, create an object off screen to the right ‘SpawnPipe’ and use that as a spawn point. also create one off screen to the left as ‘DestroyPipe’

if you create a box collider on the one thats off screen to the left. as the pipe collides, just adjust the transform to respawn with a random y val at the position of the ‘spawnpipe’ object.
that way cutting down on instantiate and destroy calls as you will only ever need say 4 pipes created at any one time, just move them around :slight_smile:

sorry for the length of post, bit longer than i initially set out to do lol
hope it helps a bit.

let me know if this is what your after if not ill ammend. cheers.