Trying to add Quaternion by having issues

Hi all,
I’m trying to add two Quaternion together, and when I move the item on a single axis it works fine however when I try and move it on multiple axis it has issues. See video.

This is my current code. The selectionOrder variable is a variable that holds all of the selected items within the editor view. In this instance its all of the cubes - you can see the selection order by looking at the #Numbers on each of the cube.

Quaternion Objectrotation = Handles.RotationHandle(selectionOrder[i].gameObject.transform.rotation, selectionOrder[i].gameObject.transform.position);

spreadObjectsRotation(selectionOrder[i].gameObject, Objectrotation);
void spreadObjectsRotation(GameObject movedObj, Quaternion moveToRotation){
    Quaternion difference = moveToRotation * Quaternion.Inverse(movedObj.transform.rotation);
    for (int i = 0; i < selectionOrder.Count; i++){
        selectionOrder[i].transform.rotation = selectionOrder[i].transform.rotation * difference;
     }
}

What am I doing wrong? Why does the rotation behave like this?

You have not said what it is you want, but the rotation you end up with for the object you moved the handle on is:

transformRotation * observedHandleRotation * inverseTransformRotation

Since quaternion multiplication is not commutative that is unlikely to be what you want as transformRotation won’t be cancelled out by its inverse. So it could be that you get what you want by changing difference to

Quaternion difference = Quaternion.Inverse(movedObj.transform.rotation) * moveToRotation;
1 Like

Perfect! That fixed it :)! Thank you

Wouldn’t it also work to just ignore this whole difference calculation thing and just set the rotations to the expected result? (note the commented out, no longer necessary first argument)

void spreadObjectsRotation(/* GameObject movedObj, */Quaternion moveToRotation){
    for (int i = 0; i < selectionOrder.Count; i++){
        selectionOrder[i].transform.rotation = moveToRotation;
    }
}

Yes it would do, however I’m also implementing a fan/spread of rotation over multiple overs. And this allows me to do that :slight_smile: