Hello !
I spent the whole day trying to understand why what I need to do (which should be pretty simple) seems impossible… I’m sorry if my question seems to be an recurrent one, but I think I’ve read every thread about it without finding a proper answer.
What I’m trying to achieve
I have a cube. I need this cube to rotate in a random direction with a random angle (that can be more than 360°). And I need this rotation movement to be “Lerp”.
So for a given duration (let’s say one second), the cube rotates and slowly stops.
My problem
I understand that Quaternions cannot achieve exactly what I want, because they cannot exceed 180° without going the other way. So my actual function works well, except that the rotation angle is quite small.
I also tried a lot of things playing with Euler Angles, but I didn’t manage either to get the result I need. No matter how I implement it, the rotation direction of the cube seems to change randomly during the Lerp, instead of turning always in the same direction (like a wheel).
So, I would like to know if there is a way to play with Quaternions and achieve this simple effect. Maybe I just don’t enough understand the way Quaternions work, and I’m missing something obvious, but… This is really driving me crazy, as it seems to be a pretty simple effect.
I hope I gave enough details, if not feel free to ask me is anything else might help. Thank you a lot in advance!
Here is the code I used to make the cube rotate (which works, but the maximum value of the random angle does not influence the rotation when it exceeds 180°). I get back to the most simple code, but I tried a bunch of much more complicated ways:
private IEnumerator Rotation()
{
// Initialize the time variables
float currentTime = 0f;
float currentDuration = 1f;
// Calculate a new random rotation
Quaternion newRotation = Quaternion.Euler(Random.Range(0f, 3600f), Random.Range(0f, 3600f), Random.Range(0f, 3600f));
while(currentTime < currentDuration)
{
currentTime += Time.deltaTime;
// Lerp the rotation
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, currentTime / currentDuration);
yield return null;
}
yield return null;
}