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;
}
}