Let’s assume I have a sphere that can hover, so it has an offset from the ground. What I want is to simply rotate the sphere to match the normals of whatever is under it on the X and Z-axis, but still be able to rotate it to the direction in which it is travelling on the Y-axis. I have tried a few things that I found here on the forum and [2] seemed promising but unfortunately, it did not work.
RaycastHit hit;
if (Physics.Raycast(body.position, Vector3.down, out hit, 10, layerMask))
{ }
Quaternion groundTilt = Quaternion.FromToRotation(Vector3.up, hit.normal);
body.rotation = Quaternion.Euler(0, currentMovementDirection.y, 0);
body.rotation = groundTilt * body.rotation;
Here is a diagram of what I want, seen from top and side views.
references:
[1] Rotating a player to match terrain slope - Questions & Answers - Unity Discussions
[2] https://forum.unity.com/threads/rotating-an-object-along-the-terrains-normal-whilst-looking-at-a-position.93570/