- Home /
How to use ScreenPointToRay for orthographic cameras
Hi! So I have the ScreenPointToRay working for perspective cameras, but how would I use it for orthographic cameras?
My code currently:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
//Do stuff
}
Answer by Namaarie · Oct 30, 2017 at 04:04 AM
Ok, so apparently if you have your camera's far clipping set to a stupidly high number, the ScreenPointToRay doesn't work properly. If you change it to a normal number, it should work properly. At least, that's what caused the problem for me.
Upvoted answer, very handy to know. I've made a space sim and needed a 3D map for the star system. Whenever the camera wasn't at rotational origin(i.e. if the player rotated the map to y'know, see stuff), the rays from ScreenPointToRay were off and therefore wouldn't select the planet I was clicking on (or any other planet for that matter).
This solution solved my problem, cheers. =)
I'd been scratching my head for like 2 days until I realized that it was working in perspective and not ortho. I brought my far clipping down to 15 instead of 3000. (which I'm pretty sure was the default) This was the answer for me, Thanks!