I am trying to adjust the position of a cursor (a transparent 3D cube) to where the mouse is pointing on a plane however, the cursor is initially set at the hit.point but begins to travel up the ray towards the camera. The strange thing is that it worked fine then suddenly starting to exhibit the strange behavior as stated with no changes to the code, cursor, camera or plane object. The intended behavior is to have the cursor follow the mouse and attach itself to the plane much like an rts cursor.
public class mouse_selection : MonoBehaviour
{
private Ray ray;
private RaycastHit hit;
public GameObject cursor;
void Start()
{
cursor = Instantiate(cursor);
}
void Update ()
{
ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit,Mathf.Infinity))
{
cursor.transform.position = hit.point;
}
}
}