1
\$\begingroup\$

Is it possible to calculate the exact camera bounds? By this I mean the white lines that represent the camera area e.g the lines in the picture:Camera bounds

I'm using Camera.main.screenToWolrdCoordinates to convert screen coordinates to world coordinates because I want to position the player exactly on that white line that the camera represents:Position Player

But when I calculate the boundaries using Camera.main.screenToWolrdCoordinates I get this:Position Player

The players legs are outside of that lower camera bound on y axis, it will also happen on x axis when I strict the players movement by using the value that I got from Camera.main.screenToWolrdCoordinates , I also have bouncing balls in my game that I want to strict by the same boundaries. So my question is how can I calculate camera bounds exactly by this I mean again these white lines that represent the camera view, I hope that my question is clear enought. Just to make it more clear, when I calculate the bounds using Camera.main.screenToWolrdCoordinates I just assign the Y value that I got from that calculation to the Y position of the player.

\$\endgroup\$
  • \$\begingroup\$ And I should pass Screen.width and Screen.height as arguments to viewport to world point right? \$\endgroup\$ – Fahir M Mar 11 '15 at 13:51
  • \$\begingroup\$ Can you be more specific please because the answer you gave me is not helping. \$\endgroup\$ – Fahir M Mar 11 '15 at 20:25
  • \$\begingroup\$ Are you checking the position of your player who is in world space? If so then use world to viewport and obtain the sprites height by accessing it through the sprite renderer component. If you character is at viewport position x:1, y:0.5, z:0 then it will be against the right edge of the camera and in the middle along the y axis... Check this tutorial out, it should help you youtube.com/watch?v=mq4BlZLReKk \$\endgroup\$ – Savlon Mar 11 '15 at 22:00
  • \$\begingroup\$ thank you for your answer, yes I am checking the position of the player in world space, the tutorial you gave me is useful but it still does not answer my question, I want when the game starts to position the player on the ground e.g that lower white line representing the camera and that's what I'm trying to calculate \$\endgroup\$ – Fahir M Mar 12 '15 at 5:28
0
\$\begingroup\$

Getting the lower boundary of the Camera in world space is easy with this little extension method:

public static float LowerBound(this Camera parent)
{
    return parent.transform.position.y - parent.orthographicSize;
}

I could give some more flexible methods, but this is simple and does exactly what you need.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Hello thank you for your answer but when I use the method that you game me I get he same result as the last picture above, the players legs are below the white line that represents the camera.... \$\endgroup\$ – Fahir M Mar 12 '15 at 5:06
  • \$\begingroup\$ @FahirM Is the player's transform centered at his feet? The transform might be at the right position, but the player's sprite and collider are offset somehow. \$\endgroup\$ – Ryan S. Mar 12 '15 at 6:36
  • \$\begingroup\$ no its not, I did not modify the transform center so I think its positioned at center. \$\endgroup\$ – Fahir M Mar 12 '15 at 6:59
  • 1
    \$\begingroup\$ @FahirM There's your problem. Either you need to center the transform at the bottom of the player, or offset the player's position accordingly. \$\endgroup\$ – Ryan S. Mar 12 '15 at 7:00
  • 1
    \$\begingroup\$ Try doing the same with an empty GameObject and see where the handles end up. I suspect Pachow is right. Also make sure you are setting the world coordinates and on the correct game object, not a parent. \$\endgroup\$ – gordonk Mar 12 '15 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.