Skip to content

Instantly share code, notes, and snippets.

@spaghettiSyntax
Created December 20, 2019 22:36
Show Gist options
  • Save spaghettiSyntax/a872c0bf89ba401faecacbdf8af0e1d5 to your computer and use it in GitHub Desktop.
Save spaghettiSyntax/a872c0bf89ba401faecacbdf8af0e1d5 to your computer and use it in GitHub Desktop.
Clamping Custom Camera
// 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