# Terrain...

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#.

I am pretty new to scripting but I recently did something similar to what you are asking except for the rotating part. If you search, there are many threads about this and they all seem to mention raycasting.

I originally knew nothing about raycasting but if you search long enough there is plenty you can learn from. My script is probably not perfect but it works for me. Hopefully it will help you with your script.

``````var prefab : Transform;
static var HighestPointOfTerrain : int = 100;
static var distancetoground = hit.distance;
static var hit : RaycastHit;

function Start()
{
if (Physics.Raycast(transform.position, -Vector3.up, hit, 125))
{
prefab.position.y = HighestPointOfTerrain - hit.distance + 1;
}
}
``````

I know about raycasts, just didnt know that you can raycast the terrain. Thanks, will try this.

This doesn’t really work that well, the object ended up inside of the ground.

I might be wrong but couldn’t you just add a Rigidbody to the object and let the physics deal with it?

-Mechy

Very, Very simple

Do a Collider.Raycast from X and Z above the terrain, towards the terrain. (this will get you the point of intersection)

Then, put your object at that point, set its up vector to the hit normal… (transform.up=raycasthit.normal) raise it a distance to make it appear on the ground.

This sets the X and Z euler rotation of the object to match the normal, and sets the Y rotation to zero. Simply adjust the Y euler to something and you have randomized object placement on your terrain.

A nifty trick, if none of the objects are the same. Do a Collider.Raycast on the object’s collider from its bottom. the distance between the object’s center and the hit should be the distance it should be above the ground.