Consider the following code snippet:
// HUG TERRAIN
var hitA : RaycastHit;
if (Physics.Raycast(transform.position + transform.forward, -transform.up, hitA)) {
var hitB : RaycastHit;
if (Physics.Raycast(transform.position - transform.forward, -transform.up, hitB)) {
transform.forward = Vector3.Lerp(transform.forward, hitA.point - hitB.point, Time.deltaTime); // check A to B vector and align forward
}
transform.position.y = hitB.point.y + (fixedSize.y / 2);
}
This code snippet does exactly as the comment says; makes my object (a cube in this case) hug the terrain, rotating around the x-axis to contour to hills. However, using this code snippet breaks my rotation, which is handled simply by the following:
transform.localEulerAngles.y += Input.GetAxis("Horizontal");
The above snippet works just fine on a flat surface. However, when I am on an incline, I noticed that this rotates my entire object along the WORLD y-axis (or more correctly, the parent’s y-axis, instead of the object’s local y-axis).
How would I go about rotating an object so that it always rotates around it’s local y-axis?