Created
December 20, 2019 22:36
-
-
Save spaghettiSyntax/a872c0bf89ba401faecacbdf8af0e1d5 to your computer and use it in GitHub Desktop.
Clamping Custom Camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OrthographicSize gets the height of the camera | |
halfHeight = Camera.main.orthographicSize; | |
// .aspect == aspect ratio on the screen | |
halfWidth = halfHeight * Camera.main.aspect; | |
bottomLeftLimit = tileMap.localBounds.min + new Vector3(halfWidth, halfHeight, 0f); | |
topRightLimit = tileMap.localBounds.max + new Vector3(-halfWidth, -halfHeight, 0f); | |
// LateUpdate is called after Update also once per frame | |
void LateUpdate() | |
// Keep the camera inside the bounds | |
transform.position = new Vector3(Mathf.Clamp(transform.position.x, | |
bottomLeftLimit.x, | |
topRightLimit.x), | |
Mathf.Clamp(transform.position.y, | |
bottomLeftLimit.y, | |
topRightLimit.y), | |
transform.position.z); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment