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. Dismiss Notice

Quaternion *= operator not working

Discussion in 'Scripting' started by anthonov, Aug 17, 2022.

  1. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    What could be wrong here ? rotation should not stay zero right ?
    Code (CSharp):
    1.        
    2. Debug.Log(previewFurniture.Template.rotation); // output (0.00000, 0.00000, 0.00000, 0.00000)
    3. previewFurniture.Template.rotation *= Quaternion.AngleAxis(90, Vector3.up);
    4. Debug.Log(previewFurniture.Template.rotation); // output (0.00000, 0.00000, 0.00000, 0.00000)
    5.  
     
  2. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    624
    You Multiply with zero - the result stays zero.

    The first quaternion is invalid. Quaternion.identity is the zero rotation, which is
    (0.00000, 0.00000, 0.00000, 1.00000)
    .
    Fix the rotation of your
    previewFurniture.Template
    and it will work as expected.

    One more thing: you probably want a rotation around the world axis and not the local axis. Quaternion multiplication relies on the order. Left = World, Right = Local.
    So you probably want:

    Code (CSharp):
    1. previewFurniture.Template.rotation = Quaternion.AngleAxis(90, Vector3.up) * previewFurniture.Template.rotation;
     
  3. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    oh I missed that. I thought quaternion was initialised to quaternion identity. thanks
     
    John_Leorid likes this.
  4. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    Thanks for the tip, but I think it is not revelant in this situation because
    Code (CSharp):
    1. previewFurniture.Template.rotation
    is not a transform rotation, it's just a value stored in template, and template is not a monobehavior class.
     
    John_Leorid likes this.