- Home /
Rotate Object to match terrain Slope
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;
}
}
}
}
Still stuck with this tried various solutions they all dont work. what i wat to achieve is the y axis is untouched but can be changed later and the x axis rotates to match the terrain normal things ive tried below
//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 = Quaternion.Euler(newRot.x, transform.rotation.y, itemClone.transform.rotation.z);
//works but is rotated the wrong way
//Vector3 upright = Vector3.Cross(itemClone.transform.up, -(Rhit.point - Lhit.point).normalized);
// itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.up, upright));
//works but cant rotate
//itemClone.transform.up = hit.normal;
//Vector3 upright = Vector3.Cross(itemClone.transform.up, hit.normal);
//itemClone.transform.rotation = Quaternion.LookRotation(upright);
//itemClone.transform.rotation = Quaternion.LookRotation(transform.up, hit.normal);
//itemClone.transform.rotation = Quaternion.LookRotation(itemClone.transform.transform.up, hit.normal);
// Quaternion originalRot = itemClone.transform.localRotation;
//itemClone.transform.localRotation = Quaternion.Euler(hit.normal.x, transform.localRotation.y, originalRot.z);
//Vector3 newRot = new Vector3(hit.normal.x,transform.rotation.y,itemClone.transform.rotation.z);
//itemClone.transform.rotation = Quaternion.LookRotation(itemClone.transform.eulerAngles, newRot);
//Vector3 originalRot = itemClone.transform.eulerAngles;
//itemClone.transform.rotation = Quaternion.LookRotation(itemClone.transform.up, hit.normal);
// itemClone.transform.rotation = new Quaternion(itemClone.transform.rotation.x,originalRot.y,originalRot.z,0);
Answer by PvTGreg · Jan 21 at 05:34 PM
Not sure how this works my brain is fried but i got it working using this incase anyone else is interested
Vector3 upright = Vector3.Cross(itemClone.transform.right, -(Rhit.point - Lhit.point).normalized);
itemClone.transform.rotation = Quaternion.LookRotation(Vector3.Cross(itemClone.transform.right, upright));
Your answer
Follow this Question
Related Questions
How to make flat object(Unity 2D) 1 Answer
Vector3.forward only moves me in 2 directions when rotating 1 Answer
Terrain tree painter lays trees horizontally on the ground 1 Answer
Checking vector3 gameObject is about to move to (pathfinding collision detection) 1 Answer
Why do grouped Terrain objects move together but don't rotate as one? 1 Answer