Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lowndes783 · Jan 17, 2019 at 10:34 AM · cameraraycastingdisplay

Raycasting with multiple cameras and displays

I'm new to Unity but know C# pretty well, I'm trying to build a demo with 4 cameras and 4 displays. Each display has it's own camera, and the cameras viewports don't overlap. I need to check if the user has clicked on items on each display, I'm using raycasting for this specifying the camera. The problem is that Input.mouseposition returns the same coords no matter which display is clicked, so items in the same position on each display register the click

e.g. display 1 has an item at x 10 y 10 and display 2 also has an item at x 10 y 10, when the user clicks on the item on display 1 the item on display 2 also thinks it's been clicked.

     private static GameObject HitTest(Camera cam)
     {
          if (Input.GetMouseButtonDown(0) && cam != null)
          {
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
     
             if (Physics.Raycast(ray, out hit, 100))
             {
                 Debug.DrawRay(ray.origin, ray.direction * 500, Color.green, 60);
     
                 // The gameobject hit
                 GameObject hitGameObject = hit.collider.gameObject;
     
                 Logger.instance.LogMessage("hit  " + hitGameObject.name);
                 return hitGameObject;
             }
         }
     
             return null;
    }


Does anyone know a way round this without relying on checking the display the mouse is currently on, as this won't work when testing in unity as everything is then on one display. Thanks for any help.

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

Answer by xxmariofer · Jan 17, 2019 at 11:07 AM

Have you tried using rather than ScreenPointToRay ViewportPointToRay? i have not used it but checking unity documentarion feels like it should work, if not maybe layering objects depending the Viewport is another option.

Comment
Add comment · Show 2 · 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 sh_code · Jan 17, 2019 at 12:59 PM 1
Share

yes, when working with multiple viewports, you need to look deeper into the difference between ScreenPoint and ViewportPoint families of functions.

avatar image lowndes783 sh_code · Jan 17, 2019 at 01:47 PM 0
Share

Thanks for the replies, this looks like the way to go. But I can't get which viewport is clicked, as Input.mousePosition is the same if you clicked on the same point on either display.

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

162 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

Related Questions

How do I get my camera to rotate around the y=0 coordinate it's looking at? 0 Answers

Security Camera Scripts? 1 Answer

Displaying multiple GUITextures On One Camera 0 Answers

Render Texture display different with its camera preview 1 Answer

Camera only displaying one object even though culling mask is set to everything 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