- Home /
Get the Scene View Position Relative to Camera View Position on Mouse Click
So I have this project I'm working on, with a top-down view on a tile-type map, where the user can click on the tiles to interact with it. Now my problem is that I cannot seem to be able to find a way in which i can get the scene view coordinates based on the clicks.
I tried using the following code, but it returns the position of the camera instead of the click.
var clickedPosition : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Help would be much appreciated!
Answer by vbbartlett · Jan 22, 2013 at 10:28 PM
That is probably because it is returning a point on the near plane of the camera. You can either change your near plane to something very close to where your are rendering objects, or raycast from you camera thru that point to your scene objects. Basically a point in mouse space is really a ray in that direction. That function can't determine where on the ray do you want the point.
I tried setting near clip distance to as close as possible to the object. It still returned the value of camera's position on click. And I have no experience working with raycasting. I don't understand it.
Looking at it more, the function returns a value based on all three x,y,z values. The Z is important. You can play with the z or use raycast depending on your situation
turns out, all i had to do was change the camera projection to Orthographic, and then set a constant Y Offset, to manage the height. Thanks, though.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Show squares in the camera 0 Answers
Camera Icon 1 Answer
How to make camera position relative to a specific target. 1 Answer