Search code examples
c#unity-game-enginespawngameobject

Operator '-' cannot be applied to operands of type 'Quaternion' and 'Quaternion'


I was trying to merge two rotation value with the datatype Quaternion in unity C# script but this error came up. I tried to search for answers but there was either unsolved question or the ones that have been answered is not working.

public GameObject Cube;
public GameObject Img;
public GameObject child;
Quaternion localRotationValue = Cube.transform.localRotation - Img.transform.localRotation;
Instantiate(child, Vector.zero,localRotationValue)

If there is any link of answer is available, that will be helpful too, as I am not able to find a proper answer.


Solution

  • Googled "How to subtract quaternions" and the first link https://forum.unity.com/threads/subtracting-quaternions.317649 has said that you can just multiply by the inverse to achieve it

    q1 = q2 * Quaternion.Inverse(q3);