How to change rotation of an object through time from startRotation to endRotation?



I have two arrays of quats: first consisting start rotation of objects and second with end rotations. Can't find a good way to smoothly change rotation from start to end. I've tried to use quat.lerp as I did usually, but it doesn't work properly for some reason - all objects end up in one location (0, 0, 0). What is the best way to do that?

If it can be aligned with another my variable (which moves from 10 to 0 over time), it would be just perfect!


  • johncaltin

    To change the rotation of an object through time from startRotation to endRotation, you can use interpolation to smoothly transition between the two values.

    Here's an example using Unity's Quaternion.Lerp function:

    Copy code
    // Define start and end rotations
    Quaternion startRotation = Quaternion.identity; // no rotation
    Quaternion endRotation = Quaternion.Euler(0, 90, 0); // rotate 90 degrees on y-axis

    // Define duration of rotation
    float duration = 2f; // 2 seconds

    // Rotate object over time
    float timeElapsed = 0f;
    while (timeElapsed < duration) {
    // Calculate interpolation factor based on how much time has elapsed
    float t = timeElapsed / duration;

    // Interpolate between start and end rotations using factor t
    Quaternion rotation = Quaternion.Lerp(startRotation, endRotation, t);
    // Apply rotation to object
    transform.rotation = rotation;
    // Wait for next frame
    yield return null;
    // Update elapsed time
    timeElapsed += Time.deltaTime;


    // Ensure object ends with the correct rotation
    transform.rotation = endRotation;
    This code will smoothly rotate an object from its initial rotation to the desired end rotation over a specified duration (in this case, 2 seconds). The rotation will be updated every frame using a coroutine, until the desired duration has elapsed. Finally, the object's rotation is set to the endRotation to ensure it ends in the correct position.Or you can ask for support to fix this issue

  • MaxHarry95
    MaxHarry95 Posts: 1
    edited September 2023 #3

    Great! Here's how you can use the time variable to smoothly interpolate between two quaternion rotations using slerp in JavaScript:

    Copy code
    // Your start and end quaternions
    var startRotation = /* Quaternion for start rotation /;
    var endRotation = /
    Quaternion for end rotation */;

    // Your time variable that goes from 10 to 0
    var timeVariable = 10; // You mentioned it goes from 10 to 0

    // Map the time variable to an interpolation factor between 0 and 1
    var t = (10 - timeVariable) / 10; // Assuming your variable ranges from 10 to 0

    // Perform spherical linear interpolation (slerp)
    var interpolatedRotation = quat.slerp(startRotation, endRotation, t);

    // Now you can apply interpolatedRotation to your objects' rotations
    // For example, if you're working with Three.js:
    // object.rotation.setFromQuaternion(interpolatedRotation);
    This code maps your time variable to an interpolation factor t between 0 and 1 and then uses quat.slerp to smoothly interpolate between the start and end rotations. Finally, you can apply the interpolated rotation to your objects.

    Make sure you replace /* Quaternion for start rotation / and / Quaternion for end rotation */ with your actual quaternion values.