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?
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)
Orthographic
That the camera did not rise above the object - the border.
You want to specify bounds for the camera, Here are some resources that help you get started:
- Calculating 2D camera bounds
- How to set boundaries with camera movement
- Limit Camera Movement on XY Axis
Hope this helps.
Thanks.
Just specify the transform does not work. In the process of playing the camera changes size
So, try to calculate bounds when the camera size changes or Update camera bounds every frame in Update.
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
Clamp your camera position when you move it.
Camera mainCamera = Camera.main;
int leftBorder = -111;
int rightBorder = 111;
int topBorder = 111;
int bottomBorder = -111;
Vector2 viewSize = new Vector2 (mainCamera.orthographicSize * mainCamera.aspect, mainCamera.orthographicSize);
mainCamera.transform.position = new Vector3 (Mathf.Clamp(mainCamera.transform.position.x, leftBorder + viewSize.x, rightBorder - viewSize.x),
Mathf.Clamp(mainCamera.transform.position.y, bottomBorder + viewSize.y, topBorder - viewSize.y), -11F);