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 The_Toller · Jun 10, 2015 at 07:10 PM · cameraraycastingviewport

Raycasting depending on viewport

I am having some issues with raycasting from cameras using ScreenPointToRay.

In my game I have the original game view, rendered by the mainCamera and I have two ortographic cameras rendering from above serving as a world and local minimap (positioned top left and bottom left at about a fifth of the screen size each). Thing is, when I select a unit in my game I want to be able to use RMB for a context sensitive action. When I right-click a spot on any of my minimaps, my selected unit should interpret this as a move action. I have solved this by raycasting from the minimapcameras agains a ground collider. However, when I right-click a visible object in my original viewport, I need my unit to either move there, mine the target if it's flagged as mineable or attack if flagged as enemy etc. This works great, the problem is that I don't understand how I should tell my script which camera I want to raycast from based on viewport. At the moment, if I right-click the center of my screen, my main camera raycast is not returning anything, but my minimap-cameras still raycast and the unit starts to move to unknown locations. This also means that if I right-click in my minimapview and click an object, I get three instances of results. From the maincamera, worlmap camera and local camera.

I want it to only cast a ray from the maincamera when I'm not right-clicking inside the minimaps viewports, and from the worldcamera if i click the worldmap etc.

Thanks in advance! :)

Comment
Add comment · Show 3
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 The_Toller · Jun 11, 2015 at 06:17 PM 0
Share

Aaaanyone?

avatar image fafase · Jun 11, 2015 at 06:20 PM 0
Share

Could you just get the x position of the camera and if less than half the screen, raycast from left camera, else use right viewport camera? Or did I miss something?

avatar image The_Toller · Jun 11, 2015 at 06:27 PM 0
Share

Hmmmm, maybe - however it feels like there should be some more sophisticated way of doing it... $$anonymous$$aybe I could use a load of if-statements to cut out the positions the viewports and comparing them to the position of the mouse, but how would anyone go about to achieve the same result with, say - an elipse or circular $$anonymous$$imap? :o

2 Replies

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

Answer by The_Toller · Jun 13, 2015 at 01:39 PM

At the moment I've "solved it" by using an interoperation of @FortisVenaliter 's suggestion.

     void CheckForObjectAndSelect()
     {
         if(Input.mousePosition.x <= Screen.width / 8.5f && Input.mousePosition.y <= Screen.height / 7.6f)
         {
             //User have clicked minimal
         }
         else
         {
             //User have clicked main viewport
         }
 }

Although this solution is working for me at the moment, another solution would be necessary if the minimap was not rectangular. In that situation I guess the best way would maybe to attach colliders to the camera object that respond to raycasts, then again - that would need me to do two raycasts for each mouse click... One for the collidercheck, and one from the camera requested, ignoring the first colliders.

Let's just hope that when the day comes when I need to set up a circular minimap, I have more scripting experience and I'm able to laugh at the stupid and clumsy way of coding I thought was the correct way. :-)

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
avatar image
0

Answer by FortisVenaliter · Jun 11, 2015 at 09:05 PM

I would do it as a single-controller else if structure.

So, have a controller for mouse clicks. When it detects one, it checks first if the mouse position is within the screen bounds of the minimap. If it is, it uses that camera. If it's not in any of the minimap areas on the screen, then you use the main camera. In this setup, the individual views would not check for a mouse click themselves, they would respond to the controller when it says they should handle a current mouse click.

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 The_Toller · Jun 12, 2015 at 07:38 AM 0
Share

Is there a read-variable for viewport bounds? How do you mean I should set up a mouse click controller?

avatar image FortisVenaliter · Jun 13, 2015 at 01:41 PM 1
Share

I mean you should have a single controller monobehaviour class that checks Input.Is$$anonymous$$ouseDown.

That class should store a reference to each $$anonymous$$imap. When it detects a mouse press, it checks with the $$anonymous$$imap instances to see if the mouse position is in their bounds, and only if it isn't does it check against the main 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

2 People are following this question.

avatar image avatar image

Related Questions

Isometric Camera : Get the displayed terrain portion. 1 Answer

WorldToViewportPoint and WorldToScreenPoint give wrong positions when VR is enabled 1 Answer

Move the camera in 2D when target reaches bounds. Is there a better way? 0 Answers

Convert UI Image size and position to Camera viewport rec. 1 Answer

Detecting camera viewport on mouse down? 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