Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DracoDevStudent · Jun 17, 2020 at 08:36 PM · cameracamerasscreentoworldpointcoordinate-systemsplitscreen

How does ScreenToWorldPoint work with split screen with multiple cameras?

I have three orthographic cameras on that I use to split the screen in the following way: alt text

So before I split the screen, I had one game running that was spawning objects randomly from the top of the screen (top of Y axis/screen) and randomly on the X axis. When I had 1 camera, I used to get the screen boundaries like this:

 screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));

Then to generate the position of the dynamically generated object I did:

  float spawnX = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
 Vector2 spanwPosition = new Vector2(spawnX, screenBounds.y);
 spanwPosition.x = Mathf.Clamp(spawnX, screenBounds.x * -1 + objectWidth, screenBounds.x - objectWidth);
 

The last part is correct the random X position of the spawned object not be half outside the screen. My problem is that now when I have 3 different cameras, I can find the their boundaries by:

 float height = 2f * gameCamera.orthographicSize;
 float width = height * gameCamera.aspect;
 screenBounds = gameCamera.ScreenToWorldPoint(new Vector3(width, height, gameCamera.transform.position.z));

but the above code for generating the position of the object does not longer work and the objects are spawning way off the camera in the world space (I am of course adjusting the camera in it). So my guess is that the starting position (0,0) for the ScreenToWorldPoint method isn't correct because maybe the (0,0) of the screen isn't on this camera? So then what is the starting point of my camera view?

I mean, I managed to do what I wanted with ViewportToWorldPoint but how does one work with ScreenToWorldPoint in such a situation with multiple cameras?

screenshot-2.jpg (320.0 kB)
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
2
Best Answer

Answer by Namey5 · Jun 18, 2020 at 04:00 AM

A couple of things.

'ScreenToWorldPoint' works using the camera's individual projection matrices and therefore resolution. When a camera covers the entire screen, Screen.width/height are identical to Camera.pixelWidth/pixelHeight. However, if your camera doesn't take up the entire dimensions of the screen then using the Screen resolution won't work. As such, it's generally more advisable to use your camera's dimensions over the screen's (and [0,0] is still the other corner regardless).

Something else to note is that the z component of the input vector to 'ScreenToWorldPoint' is a view-space depth, not the camera's actual world-space z coordinate. As such, you should pass in the distance from the camera at which you want the objects to be placed.

 //You should cache this as every access of Camera.main actually does a full search for the camera
 Camera cam = Camera.main;
 
 //Use the camera's dimensions and get a point 5 metres away from the camera (you could use cam.nearClipPlane as the z-coord to get a point at the camera's position)
 screenBounds = cam.ScreenToWorldPoint (new Vector3 (cam.pixelWidth, cam.pixelHeight, 5f));
Comment
Add comment · Show 3 · 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 DracoDevStudent · Jun 18, 2020 at 11:29 PM 0
Share

"(and [0,0] is still the other corner regardless)." Could you please elaborate on that? I didn't quite understand it. Anyways: Thanks a lot for the nice comment and help! You not only explained everything very well, but also gave me some cool tips about stuff that I did not know, like how Camera.main functions and that I should cache it, or about pixelWidth and pixelHeight, which I didn't even knew existed. So are the "camera's dimensions" actually those 2 parameters: pixelWidth and pixelHeight?

avatar image Namey5 DracoDevStudent · Jun 18, 2020 at 11:47 PM 1
Share

Yes, Screen.width/height is simply the resolution of the window the game is running in, whereas Camera.pixelWidth/pixelHeight is the actual resolution of that specific camera. As for [0,0], what I mean is that when you are working with screen space coordinates (like in ScreenToWorldPoint), the bottom left corner of the current camera's display is at coordinates [0,0] and the top right corner is at coordinates [Camera.pixelWidth, Camera.pixelHeight].

avatar image DracoDevStudent Namey5 · Jun 19, 2020 at 05:40 PM 0
Share

Ok, thanks a lot mate! :)

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

196 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

Related Questions

Need explanation for this code 1 Answer

How can I mimic this game's camera in Unity? 1 Answer

How to get a camera state to wait with coroutine? 0 Answers

Switching Between Cameras JavaScript 0 Answers

Can I make a camera that has an orthographic vertical axis, but a perspective on the horzontal axis? 1 Answer


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