I’m trying to instantiate a shot in front of my ship, but with a custom rotation relative to my ship’s local coordinates.
So I tried this:
Instantiate(shotPrefab, transform.TransformPoint(Vector3(x,y,z)), transform.rotation + Quaternion.Euler(x,y,z));
But I get this error:
BCE0051: Operator ‘+’ cannot be used with a left hand side of type
‘UnityEngine.Quaternion’ and a right
hand side of type
So it says I can’t add two quaternions.
What’s the best way to solve this problem?
Multiplying should give you the result you want.
transform.rotation * Quaternion.Euler();
I’m not sure why Peter G’s response was voted as correct or voted up, it’s simply wrong.
Multiplying a Quaternion by a vector returns another vector, and the vector you multiply by should be a direction vector (representing a direction from the origin), not a set of floats representing euler angles in degrees. The result of that multiply operation is that direction vector rotated by the Quaternion.
In order to add two quaternion rotations correctly you multiply them:
Quaternion newRotation = transform.rotation * otherTransform.rotation
To subtract you need to multiply by the inverse:
// Rotate back to the starting position
transform.rotation = newRotation * Quaternion.Inverse(otherTransform.rotation)
Alternatively, you can use Euler Angles and simply + (Add) or - (Subtract) the values then convert the result to a Quaternion.