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 Catlard · Jun 15, 2012 at 06:22 AM · raycastraycastingrayoriginscreenpointtoray

camera.ScreenPointToRay always has same origin...

Hey, folks. Got a quick question here. Can you imagine any situation in which a ray's origin is always the same, even though you're using camera.ScreenPointToRay(Input.mousePosition)? I've been pouring over my code and can't figure out why I'm not getting a ray.origin that's different with the following code...

 ray = this.gameObject.camera.ScreenPointToRay(screenPos);
 print (ray.origin);
 print (screenPos);
Comment
Add comment · Show 6
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 whydoidoit · Jun 15, 2012 at 09:23 AM 0
Share

Isn't the origin always the no$$anonymous$$al point of the viewer? Hence it is always the same for every screen point? It's a ray that passes through the screen point. Why do you need it to be different?

avatar image Catlard · Jun 15, 2012 at 10:02 AM 0
Share

Well, in the case of ScreenPointToRay, the origin is supposed to be the point on the screen, right? I've built new scenes, to test this concept, and it seems to work like this. The script reference also says it works like that.

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html

Good idea, though!

avatar image whydoidoit · Jun 15, 2012 at 10:05 AM 0
Share

I don't read that script reference as saying that :) it says it goes from the camera which has a single position - through the screen point.

avatar image Wolfram · Jun 15, 2012 at 10:28 AM 1
Share

Actually, it says "starting on the near plane of the camera".

avatar image whydoidoit · Jun 15, 2012 at 10:34 AM 0
Share

True, so it does :)

Show more comments

1 Reply

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

Answer by Wolfram · Jun 15, 2012 at 10:28 AM

It is working as expected. The docs explicitly say "starting on the near plane of the camera".

Note that the default Vector3.ToString() method rounds after the first decimal point, which could give you the wrong impression of the true values of vectors (this has been bugging me forever). Use this instead:

 print (ray.origin.ToString("G3"));
Comment
Add comment · Show 5 · 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 Catlard · Jun 15, 2012 at 10:41 AM 0
Share

Heh, perhaps we're getting into grammar :). Anyway, fair enough, but it also says it returns a ray "going through position's (x,y) pixel coordinates on the screen (position.z is ignored)." Doesn't that indicate that it should start at the point represented by that pixel value? And if ScreenPointToRay isn't how you get that point, then how DO I get the point represented by those pixel coordinates? Riddle me that, good sir.

avatar image whydoidoit · Jun 15, 2012 at 10:49 AM 0
Share

Yeah good point - I was going to suggest that you get the position on the ray that represented the near clip plane - but as @wolfram says it does say it starts on that plane. In fact it does say both :S But the clarification implies your origin should change.

avatar image Wolfram · Jun 15, 2012 at 10:56 AM 0
Share

Well, while from a general understanding, the near clipping plane is the screen, the parameter "position" is given in sceenspace, while anything related to "near plane" is 3D local space. You are right that both are talking about the same point, so it shouldn't be possible to derive a ray direction from that. $$anonymous$$aybe a more correct phrasing would be "The resulting ray's origin is the 3D position of the given pixel on the near clipping plane, while the ray's direction is deter$$anonymous$$ed by the line from the camera's position through that pixel".

avatar image whydoidoit · Jun 15, 2012 at 11:03 AM 0
Share

@wolfram Nice explanation :)

Anyway - @sgbraunstein - is there some reason you are not using Camera.ScreenToWorldPoint if you just want the world point at the near clip plane that does take it as a Z coordinate - otherwise 0 returns the camera (that was why I thought the ray did the same thing :)

avatar image Catlard · Jun 15, 2012 at 12:03 PM 0
Share

Ah! good point. I will check that out. Thanks, guys! You're a super crime-solving $$anonymous$$m.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast returns null for no apparent reason 0 Answers

Problem with raycast distance 1 Answer

[SOLVED] Raycast for custom button JS 1 Answer

Changing position of a RayCast 1 Answer

What's wrong with my RaycastHit2D? 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