Regarding ScreenToWorldPoint y 0

Hi,

I am writing a simple spawn mechanic for a game and need objects to be able to spawn at random points between the top of the screen and bottom of the screen.

However, when using the below code to define the bottom of the screen it returns a coordinate which is in the center of the screen rather than the very bottom (hence making my current fix dividing it by 2).

var fieldstart = Camera.main.ScreenToWorldPoint(Vector3(0, 0, 0)).y;

To make matters even weirder the same script contains the below code, which sets the ship object to Screen.height/2, which again places it at the center of the screen (as it should do).

Instantiate(ship,Camera.main.ScreenToWorldPoint(Vector3(170,Screen.height/2, Camera.main.nearClipPlane+25)),Quaternion(0,-180,0,0));

The code does not reset the position of the spawning objects at any point of the code, nor does any other script.

A screen point is in 2D space, and corresponds to an infinite line in the 3D world. In order to define a single point in the 3D world, you must pass in the Z coordinate the distance of the desired point relative to the camera - that’s why Camera.main.nearClipPlane+25 is passed in the second ScreenToWorldPoint. You could try to pass the same value to the first one, like this:

var fieldstart = Camera.main.ScreenToWorldPoint(Vector3(0, 0, Camera.main.nearClipPlane+25)).y;

This returns the Y coordinate of the point at the lower left screen corner that’s at 25 units from the camera’s near plane. I suspect that this isn’t exactly what you want - specially since the Y coordinate alone won’t make too much sense unless your camera is looking in the world +Z direction.