- Home /
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);
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?
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!
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.
Actually, it says "starting on the near plane of the camera".
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"));
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.
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.
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".
@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 :)
Ah! good point. I will check that out. Thanks, guys! You're a super crime-solving $$anonymous$$m.
Your answer
Follow this Question
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