So we all know that Quanternion.Lerp() moves from one rotation to the other over the shortest possible distance.
I would like to know if there is a way to make the Lerp go in the opposite direction.
My initial impulse was to find the rotation at the “halfway rotation” in the lerp (where t = 0.5) between two rotations and then reverse the rotation to face the opposite direction. With this new rotation, I would rotate from my start to that, and then from there to my end, but I have absolutely no idea how to “reverse” the angle of the rotation.
I think those solutions are weird or at least not very “quaternion” friendly.
I have written the lerp and slerp myself, with a flag to indicate whether you want to force the short way or not. I am pretty sure that Unity checks that to invert the rotation. With this code, you can indicate if you want that behaviour.
I have checked myself and it seems it works ok, but check yourself just in case:
Okay, I got this to do what I wanted. It might be a bit more annoying to pull off, but it provides that much more customization to my sword swings, so that works out.
What I did is add a third rotation for the midpoint along my path. Then, if I’ve set up the slash to use that mid rotation, I fire off this bit of code (understand that “lerpTime” is a 0 - 1 float for the lerp).
Thus, so long as the difference between startRotation and midRotation and midRotation and endRotation are each less than 180 degrees from each other, the movement looks correct. With a sword slash, this also allows me to incorporate sweeping motions and little flicks that might have otherwise been very difficult to pull off.
I hope this is useful to other folks beside me. I know doing sword slashes through code isn’t exactly a broad use case.