I implemented a terrain follow script and am having difficulty in setting the GO on the Y-axis to anything other than 0 as the script always sets the Y-axis to 0. The script looks like this:
var fwd:Vector3 = transform.TransformDirection(Vector3.forward);
transform.position += fwd/speedModifier;
var hit:RaycastHit;
if (Physics.Raycast(transform.position, -transform.up, hit, 10)){
if ( hit.distance > 1 ){
transform.position.y -= hit.distance-1;
} else if ( hit.distance < 1 ){
transform.position.y += 1-hit.distance;
}
var rot:Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
transform.rotation = rot;
}
Essentially no matter what I try, on the line where the transform.rotation gets set to rot, the Y-axis will either snap to 0, will bob violently or just will not work with some other undesirable result.
Some examples of what I tried are:
- in getting the Quaternion, I changed Vector3.up to transform.up
- in the line where I set the rotation to rot, I changed it to this transform.rotation *= rot
- myriad of other random thing
Any help here would be greatly appreciated as I am real stuck here. Thanks in advance!

