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 Nordir · Jan 03, 2013 at 03:51 PM · raycastrayhitmouse position

Physics.Raycast (ray, hit) strange behaviour

I'm making a jewels like game. I select 3D jewels with a mouse click. I do it using

 var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast (ray, hit)){ bla-bla-bla

Everything was fine until i moved the camera from z:-630 to z:-730. Now the ray hits jewels that are lower than the mouse position (if on the top of the screen, and vice versa if on the bottom). But works fine if selecting a jewel in the middle of the screen.

Is it a kind of a delta growing with camera distance?

The camera field of view is 1 (i don't want orthographic though it has the same problem).

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

3 Replies

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

Answer by Nordir · Jan 03, 2013 at 07:32 PM

While trying to draw a debug ray i discovered that my 2nd camera for background had a "MainCamera" tag (cause i had simply cloned it ofcourse). If two cameras were close everything went OK, if not - they calculated the stuff differently. Ha-ha :)

Sorry folks for disturbing you!

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
-1

Answer by Jeffom · Jan 03, 2013 at 05:45 PM

Set the screen resolution of your game screen to a fixed one

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 MibZ · Jan 03, 2013 at 04:12 PM

When dealing with Y values in screen coordinates you have to subtract the Y position from Screen.height.

Try this: var ray: Ray = Camera.main.ScreenPointToRay(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y));

Comment
Add comment · Show 9 · 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 Nordir · Jan 03, 2013 at 04:23 PM 0
Share

So if i click in the left bottom corner (0, 0) i will get Screen.height in "Screen.height - Input.mousePosition.y". Why would i want this? :)

avatar image MibZ · Jan 03, 2013 at 04:27 PM 0
Share

I don't know exactly why it works as it does, something to do with the math Unity does that we don't see, but whenever my Y coordinates are inverted this corrects the problem, whether it is dealing with converting a touchscreen tap into a 3D coordinate or placing a GUI element over a GameObject.

Try it.

avatar image Eric5h5 · Jan 03, 2013 at 04:38 PM 0
Share

You really don't need to, and shouldn't, subtract the Y from Screen.height. The only time you would ever do that is converting Input.mousePosition to GUI coordinates for use in OnGUI, but even then, you'd simply use Event.current.mousePosition rather than Input.mousePosition, so it's still not necessary. Besides, the question is asking about raycasting, which excludes OnGUI entirely.

avatar image Nordir · Jan 03, 2013 at 04:46 PM 0
Share

Eric5h5, i know u are the Unity megamonster (in a good way) :) U must have an answer to my question! :)

avatar image MibZ · Jan 03, 2013 at 04:50 PM 0
Share

I know that the question didn't ask about OnGUI, I mentioned that as a side note. His question describes that when converting screen coordinates (mouse position) to world coordinates, the Y values are inverted, and when I ran into that problem the solution I found (here on Unity Answers) said to subtract the Y value from Screen.height, and every time I have done that it has worked.

If that isn't it, what is?

Show more comments

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

10 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

Related Questions

I don't know why i can't detect by ray sth. tagged, 1 Answer

Exclude tag from raycast 1 Answer

how to get info on what raycast hitted 1 Answer

Raycast on mini-map 0 Answers

how to select an object with raycasting 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