Following Terrain Height?

EDIT: THE PROBLEM IS HIT.DISTANCE AS IT KEEPS CHANGING. I need it to be dynamic, but not be 2 things at once.

Is there a way to make an object go all the way down on the Y axis until it hits the terrain? As well as rotate with the curves?

I do not even know where to start with it , also I use javascript instead of C#, as I do not really know c#.

Extra info:
I am instantiating an object, but using the code below, it just starts flickering from the instantiated place to the area the code calls for. The code below is all that is attached to the instantiated object.

Here is the code:

function Update ()
{
var HighestPointOfTerrain : int = 50;
var hit : RaycastHit;
if(Physics.Raycast(transform.position, -Vector3.up, hit))
{
var distancetoground = hit.distance;
var heightToAdd = transform.localScale.y;
transform.position.y = HighestPointOfTerrain - distancetoground+ heightToAdd;
}
}

Try changing the line

transform.position.y = HighestPointOfTerrain - distancetoground+ heightToAdd;

to

transform.position.y = transform.position.y - distancetoground + heightToAdd;

Basically, what was happening was you were assuming that your object was always at a height of HighestPointOfTerrain (at least, that is what the code was doing), which caused your calculations to get messed up.

are you looking to put an actual object on the ground or is this like a targeting box or something along those lines that you want to simply project onto the terrain. if it’s the second idea, you might want to look into projections.

http://unity3d.com/support/documentation/Components/class-Projector.html