LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

This version of Unity is unsupported.

Quaternion Constructor

public Quaternion(x: float, y: float, z: float, w: float)

Description

Constructs new Quaternion with given x,y,z,w components.

#pragma strict
//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.
public class Example extends MonoBehaviour {
	var m_MyXm_MyYm_MyZ: float;
	public var m_SliderXm_SliderYm_SliderZ: Slider;
	public var m_TextXm_TextYm_TextZ: Text;
	// Use this for initialization
	function 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 function Change(x: float, y: float, z: float) {
		return new Quaternion(x, y, z, 1);
	}
	function 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);
	}
}