- Home /
Change width and height of camera independently of each other,Change height and width of camera without changing size of display
Hi, I am fairly new to unity and c# and I am mostly just doing this for learning purposes.
I want to make a game where the camera's width and height can change independently of each other, and rather than changing the size of the game on the screen, it maintains the same size and stretches or squishes the environment to do it. I don't know how to change the viewport rectangle values with a script and I can't figure out how to do it by looking it up, because every time I save my script it gives an error that says "object reference not set to an instance of an object". I'm sorry if this is a stupid question but I'm very inexperienced in programming and I'm a lot more used to python, so this is all new to me.
Here is my script so far:
public class CameraScale : MonoBehaviour
{
public float cScaleX = 1f;
public float cScaleY = 1f;
GameObject CameraGameObject = GameObject.Find("Camera");
Rect cameraRect = new Rect(0, 0, 1, 1);
void Update() {
cScaleX *= (1f + (0.001f * Input.GetAxisRaw("CameraScaleX")));
cScaleY *= (1f + (0.001f * Input.GetAxisRaw("CameraScaleY")));
cameraRect.width = cScaleX;
cameraRect.height = cScaleY;
cameraRect.center = new Vector2(0.5f,0.5f);
CameraGameObject.viewportRect = cameraRect;
}
}
The cScaleX and Y variables are how tall and wide the camera should be and they are controlled with the arrow keys.
Answer by Namey5 · Jul 18, 2020 at 12:08 AM
Changing the viewport won't do anything like that - you'll need to modify the camera's projection matrix;
//Cache the camera
private Camera cam;
//This is the scaling factor for each axis
[SerializeField]
private Vector2 scale = Vector2.one;
//Before any objects are drawn
void OnPreCull ()
{
//Find and store the attached camera if we don't already have it cached
if (!cam)
cam = GetComponent<Camera>();
//These functions create projection matrices for both orthographic and perspective projection. Choose between them based on the camera's settings
Matrix4x4 proj;
if (cam.orthographic)
proj = Matrix4x4.Ortho (-cam.orthographicSize * scale.x, cam.orthographicSize * scale.x, -cam.orthographicSize * scale.y, cam.orthographicSize * scale.y, cam.nearClipPlane, cam.farClipPlane)
else
proj = Matrix4x4.Perspective (cam.fieldOfView, scale.x / scale.y, cam.nearClipPlane, cam.farClipPlane);
//Set the camera's projection matrix
cam.projectionMatrix = proj;
}
void OnDisable ()
{
//*Important* Reset the camera's projection matrix when this is disabled/the scene ends or things will start to break
if (cam)
cam.ResetProjectionMatrix ();
}
https://docs.unity3d.com/ScriptReference/Matrix4x4.Ortho.html
https://docs.unity3d.com/ScriptReference/Matrix4x4.Perspective.html
For anyone using URP or HDRP this code won't work as is. You must add the following:
using UnityEngine.Rendering;
void Start() { RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering; }
Then replace OnPrecull's declaration line with: void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera)
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Set Game Object Position to Sides of a Camera 1 Answer
Is it possible to check what percentage of an object is visible in a camera's view? 0 Answers
GameObject can be seen in scene tab but cannot be seen in game tab 1 Answer
How to make gameobject follow mouse pointer that's on rotated orthographic camera? 0 Answers