Search Unity

borders for the camera

Discussion in '2D' started by sokolllll89, Oct 11, 2017.

  1. sokolllll89

    sokolllll89

    Joined:
    May 7, 2017
    Posts:
    26
    How to make borders for the camera.
    if (Screen.height> = Camera.main.WorldToScreenPoint (up.position) .y) {
    cam.transform.position = new Vector3 (cam.transform.position.x, up.position.y - (???),
    }
    What to insert instead of question marks? Or maybe I'm doing something wrong?
     
  2. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    What do you mean by borders?

    Do you want to add a collider to the camera?

    Or you want to draw a line?

    What is the type of your camera? (Orthographic or Perspective)
     
  3. sokolllll89

    sokolllll89

    Joined:
    May 7, 2017
    Posts:
    26
    Orthographic
    That the camera did not rise above the object - the border.
     
  4. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    imDanOush likes this.
  5. sokolllll89

    sokolllll89

    Joined:
    May 7, 2017
    Posts:
    26
  6. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    So, try to calculate bounds when the camera size changes or Update camera bounds every frame in Update.
     
  7. sokolllll89

    sokolllll89

    Joined:
    May 7, 2017
    Posts:
    26
    When I write
    if (Screen.height> = Camera.main.WorldToScreenPoint (up.position) .y) {
    cam.transform.position = new Vector3 (cam.transform.position.x, up.position.y - Camera.main.ScreenToWorldPoint(Screen.height).y,
    }

    Camera.main.ScreenToWorldPoint(Screen.height).y - gives an error message
     
  8. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Clamp your camera position when you move it.
    Code (csharp):
    1. Camera mainCamera = Camera.main;
    2. int leftBorder = -111;
    3. int rightBorder = 111;
    4. int topBorder = 111;
    5. int bottomBorder = -111;
    6.  
    7. Vector2 viewSize = new Vector2 (mainCamera.orthographicSize * mainCamera.aspect, mainCamera.orthographicSize);
    8. mainCamera.transform.position = new Vector3 (Mathf.Clamp(mainCamera.transform.position.x, leftBorder + viewSize.x, rightBorder - viewSize.x),
    9.             Mathf.Clamp(mainCamera.transform.position.y, bottomBorder + viewSize.y, topBorder - viewSize.y), -11F);