mouse position on terrain

hi im trying to get a Vector3 of the mouse cursors position on the terrain, and then place an object there. ive tried:

var mousePo = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Input.mousePosition.y,camera.nearClipPlane));
var ray = camera.ScreenPointToRay(mousePo);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
Instantiate (object, mousePo, Quaternion.identity);

but the ray is not drawn and although the object appears over the mouse cursor, it is too close to the screen.

thanks, ng93

You will not see the ray unless you move the camera, because you're looking along the ray.

If you want to find the intersection of that ray with the terrain (and therefore the length of that ray), you need to do a Physics.Raycast with that ray:

var hit:RaycastHit;
if(Physics.Raycast(ray.origin,ray.direction, out hit)){
    print("world point on terrain: "+hit.point+", distance to point: "+hit.distance);