四元数的 Z 分量。除非您十分了解四元数,否则不要直接进行此种修改。
//Create three Sliders (Create>UI>Slider) and three Text GameObjects (Create>UI>Text). These are for manipulating the x, y, and z values of the Quaternion. The text will act as a label for each Slider, so position them appropriately. //Attach this script to a GameObject. //Click on the GameObject and attach each of the Sliders and Texts to the fields in the Inspector.
//This script shows how the numbers placed into the x, y, and z components of a Quaternion effect the GameObject when the w component is left at 1. //Use the Sliders to see the effects.
using UnityEngine; using UnityEngine.UI;
public class Example : MonoBehaviour { //These are the floats for the x, y, and z components of the quaternion float m_MyX, m_MyY, m_MyZ; //These are the Sliders that set the rotation. Remember to assign these in the Inspector public Slider m_SliderX, m_SliderY, m_SliderZ; //These are the Texts that output the current value of the rotations. Remember to assign these in the Inspector public Text m_TextX, m_TextY, m_TextZ;
// Use this for initialization void Start() { //Initialise the x, y, and z components of the future Quaternion m_MyX = 0; m_MyY = 0; m_MyZ = 0;
//Set all the sliders max values to 1 so the Quaternion values don't go over 1 m_SliderX.maxValue = 1; m_SliderY.maxValue = 1; m_SliderZ.maxValue = 1;
//Set all the sliders min values to -1 so the Quaternion values don't go under 1 m_SliderX.minValue = -1; m_SliderY.minValue = -1; m_SliderZ.minValue = -1; }
//Change the Quaternion values depending on the values of the Sliders private static Quaternion Change(float x, float y, float z) { //Return the new Quaternion return new Quaternion(x, y , z, 1); }
void Update() { //Update the x, y and z values to that of the sliders m_MyX = m_SliderX.value; m_MyY = m_SliderY.value; m_MyZ = m_SliderZ.value; //Output the current values of x, y, and z m_TextX.text = " X : " + m_MyX; m_TextY.text = " Y : " + m_MyY; m_TextZ.text = " Z : " + m_MyZ;
//Rotate the GameObject by the new Quaternion transform.rotation = Change(m_MyX, m_MyY, m_MyZ); } }
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.