- Home /
Change camera viewport rect via script
Hi! I have a camera in my game that is being used as a minimap/gps. It's viewport rect is: X: 0 Y:0 W:0.12 H: 0.24
Do someone know how do i change it's Width and Height (I think these are the stands for W and H) to 1 via script? I want the script to do it only when the player press M and then return when the player press M again. As far as i know, i need to create a variable and then attach the camera on it later to reference it, and i need to use a function update and if getkey. The problem is that i don't know how to expand the camera after this and how to return after the player press the key.
Answer by Namey5 · Oct 24, 2016 at 07:28 AM
https://docs.unity3d.com/ScriptReference/Camera-rect.html
https://docs.unity3d.com/ScriptReference/Rect.html
The viewport rect or 'Camera.rect' takes a 'Rect' class as it's input, in normalised coordinates. You can see the access of these variables in the links above. As for expanding/returning on a button press it would be a fairly simple matter;
bool isExpanded = false;
void Update ()
{
if (Input.GetKeyDown (KeyCode.M))
isExpanded = !isExpanded;
if (isExpanded) {
//Do stuff...
} else {
//Do opposite stuff...
}
}