Hello Guest

Author Topic: What is the correct way to keep camera within tilemap bounds?  (Read 5867 times)

kgilbert2

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
What is the correct way to keep camera within tilemap bounds?
« on: February 16, 2015, 02:59:22 pm »
Hi,

I am new to Unity and 2D Toolkit, with a lot of experience with C#.  I am working on a top down 2D game, using tile maps and sprites.  I have setup the following:

Camera: 16 pixels per meter,  zoom factor of 1.
Tile Sprite / Collection:  16 pixels per meter, tiles are 128x128 pixel.
Tile Map:  15 columns, 15 rows.

Using GuiLabel, I have the following info on screen:

Tile Size X: 6.4, Y: 6.4 -  This is calculated using  tile map data.  I'm not sure if 6.4 is in units/meters.  If I multiply it by 16, is that my tile map bounds?
Cam Max X: 38.4 Y:54.4  -  If I remember correctly this is from the Camera instance's screen extents. 

I am having some trouble getting the camera to stay within the tile map bounds.  Can someone please point me in the right direction? 
« Last Edit: February 16, 2015, 05:28:11 pm by kgilbert2 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: What is the correct way to keep camera within tilemap bounds?
« Reply #1 on: February 16, 2015, 11:15:32 pm »
Quote
         Vector3 p0 = data.tileOrigin;
         Vector3 p1 = new Vector3(p0.x + data.tileSize.x * width, p0.y + data.tileSize.y * height, 0.0f);

That gives you the extents of the entire tilemap in local space (if your tilemap transform is 0,0,0 and theres no rotation or scale, that will be the same value in world space).

You'll then have to work out the camera so that tk2dCamera.ScreenExtents doesn't exceed the bounds you've calculated above.

kgilbert2

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: What is the correct way to keep camera within tilemap bounds?
« Reply #2 on: February 17, 2015, 01:18:18 am »
That worked flawlessly!  Thank you very much! :)