Help! Have object snapped to mouse going below terrain

Hello.

I am using the following code to lock a game object to the mouse. So you click build, the game object ghost locks to mouse so you can choose where to build it. The code below works fine, but I decided to change the X rotation on the main camera to 45 instead of 90. Now the object is dissappearing below the terrain when I move down vertically and raising high above the terrain when moving up vertically. HELP!

var screenSpace = Camera.main.WorldToScreenPoint(transform.position);

var curScreenSpace = Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace);

transform.position = curPosition;

There has to be a better way to snap an object to the mouse cursor… Anybody?

Figured it out on my own… Thanks for nothing…