Hello
I followed a tutorial to get the position in the game world of my mouse when i mouse button down, but it doesn’t work. It gives a position far removed from the correct position.
My floor position is 0,0,0 and i have a box collider with this code attatched:
void OnMouseDown()
{
Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log(p);
}
The result is something far from the real location like this:
My camera is angled if that matters like this:
So i have an isometric viewing angle.
How do i correct this ? Seems the tutorial i followed was wrong.
The normal way to achieve the results is via ray casting…
3D Physics - Unity Learn should give you some ideas…
If you have sometime I can make a quick example; though I doubt I will be first to show exactly how it is done…
Edit: Basically you fire a ray in the direction of the camera from the mouse position… see what object it hits, you can even get texcoords and fragment information from the ray…
1 Like
As @HellSinker suggested, raycasting is the way to go.
About:
Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
ScreenToWorldPoint expects the z-coordinate to be provided and non zero. However Input.mousePosition.z is always zero.