Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ConDotBones123 · Jul 17, 2020 at 10:56 AM · gameobjectcamera viewport

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image esco1979 · Mar 29, 2021 at 05:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges