Hello fellow Unity coders,
trying to follow a spline path (to extrude a mesh along it later on), I am having a issue which has caused me some headache for a while now:
I am using a base mesh segment which is rotated to match the path tangent/derivate. The base segments’ default normal orientation is upwards. While walking the path, the segment is rotated to match the spline tangent. This works fine, but when the tangent is pointing down / the segment is oriented downwards, this introduces orientation spinning on the Y axis when traversing the path further downwards, due to the quaternion rotation trying to find the best/shortest rotation axis.
The code used is very simple:
for (int segmentNo = 0; segmentNo < segmentCount; segmentNo++)
{
t = segmentNo * step;
arrowHandles[segmentNo].transform.position = splineCatlike.GetPoint(t);
arrowHandles[segmentNo].transform.up = splineCatlike.GetDirection(t);
}
This picture illustrates the problem. The grey quads are test segments, which should be replaced by a mesh in the future. The pink arrow is the quaternion axis on which is rotated. The red, blue, green arrows are showing the quad orientation (EditorHandle-alike, better shown in the second picture).
I am trying to keep the code for the segment rotation calculation independent / atomic (not relying on previous segment calculations), to allow the the segment calculation to carried out in random order.
I tried several approaches, some were ok, but all come with downsides (as I am still quite new to this):
- Ditching quaternion rotation and lerping along fixed forwards for forward, right, up path orientation. This gives a very stable orientation, but has ugly transitions along path curves / turns. If the transitions would be smoother, this would be fine.
- Using quaternions, but fixing the segment.forward on a world.forward (and lerping between both) fails, as the downward quaternion can have different forward orientations, depending on how the path was oriented before going into the downward slope.
- Maybe sampling the forward of the path part which is downward pointing, and keeping the segment forward aligned to the averaged forward would work. But this does not sound like clean code.
Thanks for your help,
Bamfax