- Home /
Problems with ScreenToWorldPoint in 2D game
I'm new to unity, I have a problem with my prefabs Instantiating off camera screen, I am using WorldToScreenPoint but when I run the game objects are half on and off screen view.
Also when I view game in scene view, while the game is running object spawn outside the camera view.
Can any take a look at my code and point me in the right direction
cheers
var prefab : GameObject;
function Start() {
while (true) {
var limitMin:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(0, 0, Camera.main.transform.position.y));
var limitMax:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, Screen.height, Camera.main.transform.position.y));
var spawnPos: Vector3 = Vector3(Random.Range(limitMin.x, limitMax.x), Random.Range(limitMin.y, limitMax.y));
Instantiate(prefab, spawnPos, Quaternion.identity);
yield WaitForSeconds (3);
}
}
Answer by slek120 · Mar 17, 2014 at 10:42 AM
The position of a transform is the center of that game object. You need to add half of the size to the range. Half of the size is given by bounds.extents.
var padding:Vector3 = prefab.renderer.bounds.extents;
var spawnPos: Vector3 = Vector3(Random.Range(limitMin.x + padding.x, limitMax.x - padding.x),
Random.Range(limitMin.y + padding.y, limitMax.y - padding.y));
Thanks for helping, but they still spawn outside camera view, half on screen half off, I have included the code you posted.
Have i missed something
var prefab : GameObject;
function Start() {
while (true) {
var limit$$anonymous$$in:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(0, 0, Camera.main.transform.position.y));
var limit$$anonymous$$ax:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, Screen.height, Camera.main.transform.position.y));
var padding:Vector3 = prefab.renderer.bounds.extents;
var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x + padding.x, limit$$anonymous$$ax.x - padding.x), Random.Range(limit$$anonymous$$in.y + padding.y, limit$$anonymous$$ax.y - padding.y));
//var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x, limit$$anonymous$$ax.x), Random.Range(limit$$anonymous$$in.y, limit$$anonymous$$ax.y));
Instantiate(prefab, spawnPos, Quaternion.identity);
yield WaitForSeconds (3);
}
}
Is your camera in perspective? The distance from the camera in the z-axis matters then.
I was going to mention that you use Camera.main.transform.position.y in ScreenToWorldPoint. I'm not sure but you might want to use -Camera.main.transform.position.z.
or you can do
ScreenToWorldPoint( .., .., distanceFromCamera);
spawnPos:Vector3 = Vector3( .., .., limit$$anonymous$$in.z);
But it looks pretty weird when you have a rotated camera. I don't know the answer to your question if you do that.
No the camera is in orthographic I think, take a look at the image i have included.
Also I have tried the -Camera.main.transform.position.z, same results
[1]: /storage/temp/23714-unity+2d+camera+setup.png
That is odd. I basically copied your code and it works.
#pragma strict
var prefab : GameObject;
function Start() {
while (true) {
var limit$$anonymous$$in: Vector3 = Camera.main.ScreenToWorldPoint(Vector3(0, 0, -Camera.main.transform.position.z));
var limit$$anonymous$$ax: Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, Screen.height, -Camera.main.transform.position.z));
var padding: Vector3 = prefab.renderer.bounds.extents;
var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x + padding.x, limit$$anonymous$$ax.x - padding.x), Random.Range(limit$$anonymous$$in.y + padding.y, limit$$anonymous$$ax.y - padding.y));
//var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x, limit$$anonymous$$ax.x), Random.Range(limit$$anonymous$$in.y, limit$$anonymous$$ax.y));
Instantiate(prefab, spawnPos, Quaternion.identity);
yield WaitForSeconds (3);
}
}