Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


operator *

Combines rotations lhs and rhs.
Read time 2 minutesLast updated 11 days ago

Definition

  • Type: Operator
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

operator *(Quaternion, Quaternion)

Combines rotations
lhs
and
rhs
.
public static Quaternion operator *(Quaternion lhs, Quaternion rhs)

Parameters

Left-hand side quaternion.

Right-hand side quaternion.

Returns

Type

Description

Quaternion

Remarks

Rotating by the product
lhs
*
rhs
is the same as applying the two rotations in sequence:
lhs
first and then
rhs
, relative to the reference frame resulting from
lhs
rotation. Note that this means rotations are not commutative, so
lhs
*
rhs
does not give the same rotation as
rhs
*
lhs
.

Examples

using UnityEngine;public class QuaternionProduct : MonoBehaviour{ void Start() { // Two simple rotations Quaternion rotX = Quaternion.AngleAxis(90, Vector3.right); Quaternion rotY = Quaternion.AngleAxis(90, Vector3.up); // Order matters: lhs * rhs does not equal rhs * lhs Quaternion result1 = rotX * rotY; // applies Y rotation first, then X Quaternion result2 = rotY * rotX; // applies X rotation first, then Y Debug.Log($"X * Y = {result1.eulerAngles}"); Debug.Log($"Y * X = {result2.eulerAngles}"); // Apply one result to see the difference transform.rotation = result1; }}

operator *(Quaternion, Vector3)

Transform the Vector3
point
using
rotation
quaternion.
public static Vector3 operator *(Quaternion rotation, Vector3 point)

Parameters

rotation

Quaternion reprensenting rotation to apply.

point

Vector3 to rotate.

Returns

Type

Description

Vector3

Examples

using UnityEngine;using System.Collections;public class Example2 : MonoBehaviour{ private void Start() { //Creates an array of three points forming a triangle Vector3[] points = new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(0, 1, 0) }; //Creates a Quaternion rotation of 5 degrees around the Z axis Quaternion rotation = Quaternion.AngleAxis(5, Vector3.forward); //Loop through the array of Vector3s and apply the rotation for (int n = 0; n < points.Length; n++) { Vector3 rotatedPoint = rotation * points[n]; //Output the new rotation values Debug.Log("Point " + n + " rotated: " + rotatedPoint); } }}