Skip to content

Instantly share code, notes, and snippets.

@rdeeb
Created March 17, 2016 03:54
Show Gist options
  • Save rdeeb/0c07916239f0f9ffeeb1 to your computer and use it in GitHub Desktop.
Save rdeeb/0c07916239f0f9ffeeb1 to your computer and use it in GitHub Desktop.
Get the viewport size of an orthographic camera in unity
public class BoundryFinder : MonoBehaviour
{
public float xMin, xMax, zMin, zMax;
void Start()
{
GameObject sceneCamObj = GameObject.Find( "Main Camera" );
Camera mainCam = sceneCamObj.GetComponent<Camera> ();
Transform camPos = mainCam.transform;
float vertExtent = mainCam.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height;
zMin = -vertExtent + camPos.position.z + 0.5f;
zMax = vertExtent + camPos.position.z - 0.5f;
xMin = -horzExtent + 0.5f;
xMax = horzExtent - 0.5f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment