I am trying to a fence object to rotate to match the terrian.

i can get this to work by rotating the object using the results of one raycast but when i rotate the object later on the y axis it starts to rotate the wrong way. so i was trying to instead use two raycasts and rotate it to match the normals of those. but i cant seem to get it to work at all. it just seems to rotate wildly and im not sure where i am going wrong.

below is my current code and a picture of what i am trying to acheive

```
if (matchTerrain)
{
RaycastHit Rhit;
if (Physics.Raycast(rightRay.position, -Vector3.up, out Rhit))
{
RaycastHit Lhit;
if (Physics.Raycast(leftRay.position, -Vector3.up, out Lhit))
{
//Vector3 upright = Vector3.Cross(itemClone.transform.right, -(Rhit.point - Lhit.point).normalized);
//itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.right, upright));
//Vector3 upright = Vector3.Cross(transform.right, -(Rhit.point - Lhit.point).normalized);
//itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, upright));
Vector3 upright = Vector3.Cross(itemClone.transform.up, -(Rhit.point - Lhit.point).normalized);
Quaternion newRot = Quaternion.FromToRotation(itemClone.transform.up, upright) * itemClone.transform.rotation;
itemClone.transform.rotation = newRot;
if (addToRotation)
{
itemClone.transform.rotation = itemClone.transform.rotation * rotationChange;
}
}
}
}
```