var allowedLayer :LayerMask = -1;
var offsetFromTerrain : float;
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0,allowedLayer))
{
transform.position.y = hit.point.y + offsetFromTerrain;
}
}
And here is the result
Watch it. And you’ll know what my problem is
If all you want is the ability to place the camera a certain amount above the terrain, why not use a collider of some sort?
You could just place the camera as the child of a cylinder and give the cylinder constraints for rotating on the x and z axis. Then move the camera up by your offset from the terrain variable. Anytime you want to move the camera give it a push through a rigidbody connected to the cylinder.
if you’re trying to orient the object to “walk” on the terrain, just cast a ray downwards, get the normal of the surface from the hitInfo, and rotate your object so its up vector points in the same direction as the normal