I have a plane GameObject
on which I want to place cubes. The user can move the cube around on the plane and when LMB is clicked, the cube is placed at that position. Everything works great, besides the position of the cube is not that accurate. In the center of my plane, mouse pointer and cube position match perfectly, moving the mouse along the x-Axis also works fine, but up or down (z-axis in game world) doesn't work well, the mouse moves "more" than the cube object. Here's photos of the issue:
and here's my source code:
void Update()
{
Vector3 m = Input.mousePosition;
Vector3 n = Camera.main.ScreenToWorldPoint(new Vector3(m.x, m.y, 1));
float camDistance = (float)Math.Sqrt((Math.Pow(n.y, 2.0f) + Math.Pow(6, 2.0f)));
Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(m.x, m.y, camDistance));
cube.position = new Vector3(p.x, 0.3f, p.z);
}
At first I used camDistance
as a constant that doesn't change (distance from the camera to the center of the plane), then I tried to adjust it depending on where the object is (distance from the camera to the mouse position in world coordinates). It still doesn't look great, so I need some help.
Thank you!