Get the viewport size of an orthographic camera in unity
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
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