- Home /
Why are my camera.viewporttoworldpoint coordinates different from my actual camera viewpoint?
Sorry for the bad title. I don't know how to describe this problem, as it is very new to me.
I've written a code to show the coordinates of my 'Camera.main.ViewportToWorldPoint'.
void DrawCameraViewBox()
{
Debug.DrawLine(Camera.main.ViewportToWorldPoint(new Vector2(1, 1)), Camera.main.ViewportToWorldPoint(new Vector2(-1, 1)));//top
Debug.DrawLine(Camera.main.ViewportToWorldPoint(new Vector2(-1, 1)), Camera.main.ViewportToWorldPoint(new Vector2(-1, -1)));//left
Debug.DrawLine(Camera.main.ViewportToWorldPoint(new Vector2(-1, -1)), Camera.main.ViewportToWorldPoint(new Vector2(1, -1)));//bottom
Debug.DrawLine(Camera.main.ViewportToWorldPoint(new Vector2(1, -1)), Camera.main.ViewportToWorldPoint(new Vector2(1, 1)));//right
}
This displays a white box that should be of the same length as my camera perspective, like this:
The box from the upper right shows what my Main camera can see. And the white box surrounding that box, is generated from the code which shows the coordinates if converted from viewport to world point.
I'm supposed to write a random spawning algorithm that spawns objects just outside of the camera's perspective, but I've noticed that the objects spawns so distant from my camera. So I've used this code to investigate the problem.
So yeah, is there any way to fix the viewporttoworldpoint coordinates? or is it just in my codes? And will this affect my game in full screen?
Answer by robertbu · Sep 08, 2014 at 02:45 PM
Viewport coordinates go from (0,0) in the lower left corner to (1,1) in the upper right. '-1' is outside of the viewport.
Ohhhh. $$anonymous$$an, I felt stupid. I thought the origin starts at the center of the camera. Anyways, I've fixed my code now and everything spawns as supposed to. Thank you for your response.