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 Omberone · Jun 27, 2014 at 04:30 PM · camerasplitscreen

How can I override camera.rect?

Hi! I've been making a script that takes two cameras and draws them in a split screen fashion. They are both focused on a seperate object in the world ( parented under them ), it looks like this in game view:

http://imgur.com/dLsfiVb

Then, when I prompt it to, it will slide the two screen rectangles so that they align with a third camera - a top down view, which looks like this:

http://imgur.com/RIl60hV

Problem is when the two rectangles meet the edge of the screen, they shrink. camera.rect.x is clamped to 0 or screen.width, same with y. This distorts the content within the rectangles, making the objects smaller and centered in the rectangles as shown here

http://imgur.com/tKpWpHH

Any ideas on how to prevent this from happening?

Comment
Add comment · Show 1
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 PAEvenson · Jun 27, 2014 at 06:57 PM 0
Share

you need to clamp camera.rect.x to screen.width - camera.rect.width.

 camera.rect.x = $$anonymous$$athf.Clamp(camera.rect.x, 0, screen.width - camera.rect.width);

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Omberone · Jun 28, 2014 at 04:03 PM

I managed to solve it using this tidbit of code:

 Vector2 viewportPos = mainCamera.WorldToViewportPoint( myObjectToFocus.transform.position );
 float newX = Mathf.Lerp ( camera.rect.x, viewportPos.x - camera.rect.width / 2, Time.deltaTime * myCameraAdjustSpeed );
 float newY = Mathf.Lerp ( camera.rect.y, viewportPos.y - camera.rect.height / 2, Time.deltaTime * myCameraAdjustSpeed );
 float screenSizePercentage = new Vector2(camera.rect.width, camera.rect.height).magnitude / new Vector2(mainCamera.rect.width, mainCamera.rect.height).magnitude;
 float newSize = mainCamera.orthographicSize * screenSizePercentage;
 Rect newRect = new Rect( newX, newY, camera.rect.width, camera.rect.height );
 Vector2 wantedSize = new Vector2( Screen.width * newRect.width, Screen.height * newRect.height );
 Vector2 missingPercentage = new Vector2( camera.pixelWidth / wantedSize.x, camera.pixelHeight / wantedSize.y );
 Vector2 wantedPos = new Vector2( Screen.width * newX, Screen.height * newY );
 float aspect = wantedSize.x / wantedSize.y;
 float forcedAspect = camera.pixelWidth / camera.pixelHeight;
 wantedPos.x = newSize * ( missingPercentage.x - 1f ) * (wantedPos.x < 0 ? -1f : 1f);
 wantedPos.x *= aspect;
 wantedPos.y = newSize * ( missingPercentage.y - 1f ) * (wantedPos.y < 0 ? -1f : 1f);
 wantedPos.y /= aspect/mainCamera.aspect;
 transform.position = new Vector3(wantedPos.x, transform.position.y, wantedPos.y) + myObjectToFocus.transform.position;
 camera.orthographicSize = newSize / ( forcedAspect > aspect ? (forcedAspect / aspect) / missingPercentage.x : 1f / missingPercentage.y);
 camera.rect = newRect;

I did the math using pen and paper beforehand. Jikes it was tough. If Unity hadn't forced the pixelRect everytime you set it, it would've been much easier =)

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Change cameras visible area in camera viewport 0 Answers

Render Splitscreen to RenderTexture 0 Answers

How to make camera position relative to a specific target. 1 Answer

How to do a 4P split screen internet multiplayer 2 Answers

Having Issues with Camera Viewport Rect 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