Target: Get the point of intersection of the raycast with the plane's Layer.
The raycast draws a line that intersects the plane (on mouse click).
But the line also intersects everything in its path, so the outcome point is giving the point of intersection with objects other than the plane.
I have assigned the layer of the plane as "Plane" and included in the code the layer of the plane only when raycasting.
if (Input.GetMouseButton(0))
{
RaycastHit hit;
int layerMask = (1 << 8); // Plane's Layer
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, layerMask)) {
transform.position = hit.point;
}
}
What is happening is that the position of the gameObject is overriding its old position until the object is clipping with the camera.