Hello Guest

Author Topic: Getting camera's viewable width/height in Unity units  (Read 6169 times)

rxmarcus

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
Getting camera's viewable width/height in Unity units
« on: January 04, 2014, 10:03:22 pm »
Hey guys,
I am making a multiplayer game similar to Smash Brothers.  My game has multiple players on the same screen at once, and I am trying to make the camera zoom in and out as players are close together or farther apart from each other.

I intend to simply gather the minimum and maximum X,Y values from each player so I can find the X and Y distances that the farthest out players are in the game, and then need to use that information to zoom my camera in or out.

For example, if player 1 is at x = 50 and player 2 is at x = 250, then my camera needs to zoom to a point that views around 200 unity units.  Does anyone have an idea of how I can determine how many unity units my tk2dCamera is viewing at any given time?

More likely than not I'm thinking about this in the wrong way, so any suggestions or ideas are welcome as well.  Thank You.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting camera's viewable width/height in Unity units
« Reply #1 on: January 04, 2014, 11:18:13 pm »
tk2dCamera.ScreenExtents give you the extents of the screen local to the camera.
Or if you're using a normal unity camera (this works with a tk2dCamera too), you can use Camera.ViewportToWorldPoint to work out the 4 extrema of the screen in world space.

rxmarcus

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Getting camera's viewable width/height in Unity units
« Reply #2 on: January 04, 2014, 11:57:25 pm »
So if I use the "tk2dCamera.ScreenExtents", what would be the correct syntax to get this property?

Am attempting this currently: Rect rightPoint = thisCamera.ScreenExtents ("right");

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting camera's viewable width/height in Unity units
« Reply #3 on: January 05, 2014, 12:05:46 am »
ScreenExtents returns a Rect.
Rect rect = thisCamera.ScreenExtents;
Rect is a Unity class, and you can use it to get the min and maximum extrema / construct your own points.
You can look at how tk2dCameraAnchor.UpdateTransform works - that anchors gameObjects onto different parts of the screen.