Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help finding tilemap dimensions, creating camera bounds

Discussion in '2D' started by cathat00, Jun 18, 2019.

  1. cathat00

    cathat00

    Joined:
    Apr 7, 2018
    Posts:
    6
    Hey, folks! Beginner to Unity here, trying to figure out how to create a camera that follows the player but won't move beyond the extents of the tilemap level on the X and Y axes specifically. The way I am trying to go about this is to clamp the camera's transform to max and min values for each axis. This seems to be (correct me if I am wrong) the most straightforward way about this.

    The tilemap in question is a simple 2x2, with sprites 200 pixels by 200 pixels. In order to get the max and min values I need, I'm trying to find the coordinates of the bottom left and top right tiles on the tilemap. Everything I have seen online thus far has suggested using tilemap.origin and tilemap.size for this. The docs on tilemap.size claim that the variable stores the size of the tilemap in tiles, but only takes into account the placed tiles on the tilemap. Where I would expect this value to be something along the lines of (2, 2, 0), it is actually (16, 5, 1). What's more, where I expect the value stored in tilemap.origin to be (0,0,0) it is actually (-4, -2, 0).

    Needless to say, this screws up my calculations for the camera bounds. Tilemap.CellToWorld(tilemap.origin) returns a world point entirely separate from the bottom left corner of the map, and Tilemap.CellToWorld(tilemap.origin + tilemap.size) returns one entirely separate from the top right.

    Help?
     
  2. DavidNLN

    DavidNLN

    Joined:
    Sep 27, 2018
    Posts:
    83
    Hya, sounds like what you need is the Cinemachine 2d follow camera, it also allows to restrict it by setting bounds
    Look at this official unity vid
     
    amazingamazing and cathat00 like this.
  3. cathat00

    cathat00

    Joined:
    Apr 7, 2018
    Posts:
    6
    You are the bomb! Thanks so much for the help!