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 ishan00kaushik · Apr 02, 2015 at 08:51 PM · cameraraycastplanemouseclickselection

Arial camera get selected point

Hi all,

I have a camera which is like an Arial Camera. I am trying to select the point from mouse click but the behavior is not predictable. I am using following code to get the selected coordinates -

 Vector3 targetpos;
 float myPoint;
 private Plane plane = new Plane(Vector3.up,0);

 if (Input.GetMouseButtonDown (0)) {
                     Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
                     if (plane.Raycast (ray, out myPoint)) {
                             targetpos = ray.GetPoint (myPoint);
                     }
             }

The problem is its not giving me the correct point. Is it the correct way to get the mouse clicked coordinate from arial camera? What changes should i do for getting correct solution , because this solution works correctly when applied for third person camera view.

I have also tried Physics.Raycast but both gives me a selection away from my mouse click region.

Little more explanation - I am trying to build a AI mover , now to move from its own location to other location it need the point (x,y,x) to travel to. This "travel to" point i am trying to select from a camera view (in this case camera is situated in the sky proving static view of the model) . Now with the above mentioned code i am trying to get my "travel to" point. It do give me point selected (x,y,z) but not at the exact spot where i selected it but elsewhere.

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 ian_sorbello · Apr 02, 2015 at 09:06 PM 0
Share

I'm not 100% sure of the problem - you are looking to get the coordinates on the 'plane' object you have, hence why you have plane.Raycast ?

Which particular point are you seeking to get?

avatar image maccabbe · Apr 02, 2015 at 09:23 PM 0
Share

Your question is confusing. First, I think you mean to use the word aerial not arial. $$anonymous$$ore importantly, you should define what point you expect to get. It would also help to know how the variables plane and myPoint are defined.

Do you want a point on the screen in screen coordinates (such as the one provided by Input.mousePosition) or do you want a point in world coordinates? If you want a point in world coordinates do you want the nearest point, a point a certain distance away from the camera, a point along the created ray that intersects with a plane, or a point along the created ray that intersects with a collider? All of these have different values and would use varying methods (Camera.ScreenToWorldPoint, Camera.ScreenPointToRay.GetPoint, Plane.Raycast, Physics.Raycast).

avatar image maccabbe · Apr 02, 2015 at 10:14 PM 1
Share

The code should work (assu$$anonymous$$g you are trying to get a point on the xz plane). However you wrote the solution works correctly when applied for the third person camera but not the aerial camera. Is the aerial camera always the only or the main camera?

If you are using a second camera then Camera.mainCamera.ScreenPointToRay (Input.mousePosition); returns the ray from the first camera tagged $$anonymous$$ainCamera, which might not be the aerial camera. To fix this, you would need to do something like

 Vector3 targetpos;
 float myPoint;
 private Plane plane = new Plane(Vector3.up,0);
 public Camera aerialCamera;
  
 if (Input.Get$$anonymous$$ouseButtonDown (0)) {
     Ray ray = aerialCamera.ScreenPointToRay (Input.mousePosition);
     if (plane.Raycast (ray, out myPoint)) {
         targetpos = ray.GetPoint (myPoint);
     }
 }

assigning aerialCamera via script or in the inspector

If you always have one camera then how are the third person and aerial cameras different?

2 Replies

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

Answer by sparkzbarca · Apr 02, 2015 at 09:13 PM

  if (Input.GetMouseButtonDown (0)) {
                      Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
                      if(Physics.Raycast (ray, out myPoint)) {
                              targetpos = mypoint.point;
                      }
              }

notice

physics.raycast

and targetpos = mypoint.point

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 ishan00kaushik · Apr 02, 2015 at 09:49 PM 0
Share

Didn't worked , facing same problem still

avatar image
0

Answer by ishan00kaushik · Apr 02, 2015 at 10:38 PM

Thanks , worked like a charm . My mistake had two camera active.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Object Selection with large number of objects 1 Answer

Push object in direction of camera 0 Answers

Raycast Object Selection 3 Answers

Mouse plane does not detect height 1 Answer

Alternative to ScreenPointToRay needed 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