Version: 2020.1
public float w ;

描述

四元数的 w 分量。请勿直接修改四元数。

四元数由 4 个实数定义,可以表示 3D 旋转。xyz 表示矢量。w 是一个标量,用于存储围绕矢量进行的旋转。可以在 https://scriptinghelpers.org/blog/how-to-think-about-quaternions 中找到有关四元数的更多详细信息。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Quaternion-w script example // Create a Sphere and apply a texture to help the orientation be recognised. // At each second the sphere is rotated and the quaternion is displayed.

public class ExampleClass : MonoBehaviour { private float timeDelay = 0.0f; private Quaternion q; private string label = "";

void Awake() { // Add a line that passes through the y axis of the sphere and make // the line as a child. GameObject line = GameObject.CreatePrimitive(PrimitiveType.Cube); line.transform.localScale = new Vector3(0.05f, 1.5f, 0.05f); line.transform.parent = gameObject.transform; }

void Update() { if (timeDelay > 1.0f) { Vector3 v;

// generate a random euler angle v.x = Random.Range(0.0f, 360.0f); v.y = Random.Range(0.0f, 360.0f); v.z = Random.Range(0.0f, 360.0f);

// convert the euler into a quaternion q = Quaternion.Euler(v);

// and apply it to the sphere transform.rotation = q;

// generate a string label = q.ToString("f3");

// and start another 1 second delay timeDelay = 0.0f; } timeDelay += Time.deltaTime; }

// display the quaternion as a string void OnGUI() { GUI.skin.label.fixedHeight = 40; GUI.skin.label.fontSize = 24; GUI.Label(new Rect(10, 10, 400, 30), label); } }