Terrain height raycasting?

I’m quite new to javascript and c# and I don’t really care in which the following code should be programmed.

I have a gameobject that follows my mouse, just floating in 3d space. I want to make it stick to the ground which is a terrain object. It has come to my understanding that the best way to do this is to raycast for the y on the terrain, but I have no clue how to get this done. Can anyone help me on this subject?

This is the code I have so far (Javascript):

var depth = 10.0;

function Update ()

{

 var mousePos = Input.mousePosition;

 var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, (mousePos.y, depth));

 transform.position = wantedPos;

}

I guess I have to change the mousePos.y to the variable in which I store the terrain height or something. Any ideas?

What you should use is this:

RaycastHit hit;
if(Camera.main.ScreenPointToRay(Input.mousePosition, out hit))
{
    transform.position = hit.point;
}

If your box has a collider, you may want to put it on a different layer, and include a layermask in the raycast so that it ignores the box.