Search Unity

Question Multiply 2 quaternions with Visual Scripting

Discussion in 'Visual Scripting' started by Dr-Nick, Oct 15, 2023.

  1. Dr-Nick

    Dr-Nick

    Joined:
    Sep 9, 2013
    Posts:
    25
    I'm wanting to rotate a rigidbody by a certain amount per frame using Visual Scripting.

    The way you do this in c# is something like this.

    Code (CSharp):
    1.     Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.fixedDeltaTime);
    2.         m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
    However, I cannot see a way to multiple 2 quaternions together. Is there something I'm missing or an alternative way to rotate a rigidbody.
     
  2. termway

    termway

    Joined:
    Jul 5, 2012
    Posts:
    84
    Hello there,

    I did not find a multiply unit for Quaternion (I'm not sure why there is no such unit) but you can use the generic multiply unit (Math/Generic/Multiply)
    upload_2023-11-2_16-57-41.png

    Something like this should work:

    upload_2023-11-2_16-58-41.png

    Another solution is just to create a custom unit to multiply two quaternions together (derived from the native Vector4Multiply unit). And you can also create another custom unit to multiply a scalar with a Vector3.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Unity.VisualScripting
    4. {
    5.     /// <summary>
    6.     /// Returns the product of two quaternions.
    7.     /// </summary>
    8.     [UnitCategory("Math/Quaternion")]
    9.     [UnitTitle("Multiply")]
    10.     public sealed class QuaternionMultiply : Multiply<Quaternion>
    11.     {
    12.         protected override Quaternion defaultB => Quaternion.identity;
    13.  
    14.         public override Quaternion Operation(Quaternion a, Quaternion b)
    15.         {
    16.             return a * b;
    17.         }
    18.     }
    19. }
    20.  
    upload_2023-11-2_17-6-0.png
     
    Last edited: Nov 3, 2023
    rell023 likes this.