四元数的 w 分量。请勿直接修改四元数。
四元数由 4 个实数定义,可以表示 3D 旋转。x、y 和 z 表示矢量。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); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.