Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Trying to add Quaternion by having issues

Discussion in 'Scripting' started by JoelLB, May 28, 2020.

  1. JoelLB

    JoelLB

    Joined:
    Oct 9, 2019
    Posts:
    25
    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.

    Code (CSharp):
    1.  
    2. Quaternion Objectrotation = Handles.RotationHandle(selectionOrder[i].gameObject.transform.rotation, selectionOrder[i].gameObject.transform.position);
    3.  
    4. spreadObjectsRotation(selectionOrder[i].gameObject, Objectrotation);
    5.  
    Code (CSharp):
    1.  
    2. void spreadObjectsRotation(GameObject movedObj, Quaternion moveToRotation){
    3.     Quaternion difference = moveToRotation * Quaternion.Inverse(movedObj.transform.rotation);
    4.     for (int i = 0; i < selectionOrder.Count; i++){
    5.         selectionOrder[i].transform.rotation = selectionOrder[i].transform.rotation * difference;
    6.      }
    7. }
    8.  
    What am I doing wrong? Why does the rotation behave like this?
     
  2. Cannist

    Cannist

    Joined:
    Mar 31, 2020
    Posts:
    64
    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
    Code (CSharp):
    1. Quaternion difference = Quaternion.Inverse(movedObj.transform.rotation) * moveToRotation;
     
    JoelLB likes this.
  3. JoelLB

    JoelLB

    Joined:
    Oct 9, 2019
    Posts:
    25
    Perfect! That fixed it :)! Thank you
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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)
    Code (CSharp):
    1. void spreadObjectsRotation(/* GameObject movedObj, */Quaternion moveToRotation){
    2.     for (int i = 0; i < selectionOrder.Count; i++){
    3.         selectionOrder[i].transform.rotation = moveToRotation;
    4.     }
    5. }
     
  5. JoelLB

    JoelLB

    Joined:
    Oct 9, 2019
    Posts:
    25
    Yes it would do, however I'm also implementing a fan/spread of rotation over multiple overs. And this allows me to do that :)