Unity3D Quaternion各属性和函数測试
Posted ldxsuanfa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D Quaternion各属性和函数測试相关的知识,希望对你有一定的参考价值。
一,属性:
x、y、z就不说了,仅仅看一个eulerAngles。代码例如以下:
- public Quaternion rotation = Quaternion.identity;
- void Start()
- {
- rotation.eulerAngles = new Vector3(0, 30, 0);
- transform.rotation = rotation;
- print(rotation.eulerAngles.y);
- }
物体rotation = (0,30,0)。可是不能直接为transform.rotation.eulerAngle赋值。
二。方法
1,function ToAngleAxis (out angle :float, out axis :Vector3) :void
- public float angle = 0.0F;
- public Vector3 axis = Vector3.zero;
- void Start()
- {
- transform.eulerAngles = new Vector3(0, 90, 0);
- transform.rotation.ToAngleAxis(out angle, out axis);
- print(angle + " " + axis);
- }
输入:transform.localEulerAngles=(0,0,0);
输出:angle=0, axis=(1,0,0);
输入:transform.localEulerAngles=(0,90,0);
输出:angle=90,axis=(0,1,0);
输入:transform.localEulerAngles=(270,0,0);
输出:angle=90,axis=(-1,0,0)
2。function SetFromToRotation (fromDirection :Vector3, toDirection :Vector3) :void
- private Vector3 _from = Vector3.one;
- private Vector3 _to = new Vector3(100f, 200f, 100f);
- private Quaternion q;
- private Vector3 headUpDir;
- void Start()
- {
- q.SetFromToRotation(_from, _to);
- transform.rotation=q;
- headUpDir=transform.TransformDirection(Vector3.forward);
- }
输入:a=Vector3(0,0,1); b=Vector3(0,1,0)//把z轴朝向y轴
输出: q=(-0.7,0,0,0.7); headUpDir=(0,1,0)
输入:a=Vector3(0,0,1); b=Vector3(1,0,0)//把z轴朝向x轴
输出: q=(0,0.7,0,0.7); headUpDir=(1,0,0)
输入:a=Vector3(0,1,0); b=Vector3(1,0,0)//把y轴朝向x轴
输出: q=(0,0,-0.7,0.7); headUpDir=(0,0,1)
3,function SetLookRotation (view : Vector3, up : Vector3 = Vector3.up) : void
- public Transform obj1;
- public Transform obj2;
- Quaternion q;
- void Update()
- {
- q.SetLookRotation(obj1.position, obj2.position);
- transform.rotation = q;
- }
4, static operator * (lhs : Quaternion, rhs : Quaternion) : Quaternion
- public Transform extraRotation;
- void Update()
- {
- transform.rotation *= extraRotation.rotation;
- }
5,static operator * (rotation : Quaternion, point : Vector3) : Vector3
- Vector3 relativeDirection = Vector3.forward;
- Vector3 absoluteDirection;
- void Update () {
- absoluteDirection = transform.rotation * relativeDirection;
- transform.position += absoluteDirection * Time.deltaTime;
- }
效果:将一个向量依据rotation旋转到还有一个向量。
6。static function Dot (a : Quaternion, b : Quaternion) : float
效果:将两个Quaternion点乘,返回一个float值,意义待补充。
7,static function AngleAxis (angle : float, axis : Vector3) : Quaternion
- void Start(){
- transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
- }
8。static function FromToRotation (fromDirection :Vector3, toDirection :Vector3) : Quaternion
- transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);
9,static function LookRotation (forward :Vector3, upwards :Vector3 =Vector3.up)
: Quaternion
效果:跟SetLootRotation几乎相同,差别是能够返回一个Quaternion。
10,static function Slerp (from : Quaternion, to : Quaternion, t :float) : Quaternion
- public Transform from;
- public Transform to;
- public float speed = 0.1F;
- void Update() {
- transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
- }
11,static function Lerp (from : Quaternion, to : Quaternion, t :float) : Quaternion
效果:与Slerp效果几乎相同,效率比Slerp高可是假设from和to相差过大效果会不好,会返回一个标准化的Quaternion。
12,static function RotateTowards (from : Quaternion, to : Quaternion, maxDegreesDelta :float) : Quaternion
效果:旋转一个角度从from向to。与Slerp基本同样,但这个函数的角速度永远不会超过maxDegreesDelta。负的maxDegreesDelta值将使旋转远离to。
13。static function Inverse (rotation : Quaternion) : Quaternion
效果:返回反向的旋转。
14,static function Angle (a : Quaternion, b : Quaternion) :float
- public Transform target;
- void Update(){
- float angle = Quaternion.Angle(transform.rotation, target.rotation);
- }
效果:返回a和b两者之间的角度。
15,static function Euler (x :float, y :float, z :float) : Quaternion .
- public Quaternion rotation = Quaternion.Euler(0, 30, 0);
效果:把旋转角度变成相应的Quaternion。
三,实例
- var target : Transform;
- var edgeBorder = 0.1;
- var horizontalSpeed = 360.0;
- var verticalSpeed = 120.0;
- var minVertical = 20.0;
- var maxVertical = 85.0;
- private var x = 0.0;
- private var y = 0.0;
- private var distance = 0.0;
- function Start()
- {
- x = transform.eulerAngles.y;
- y = transform.eulerAngles.x;
- distance = (transform.position - target.position).magnitude;
- }
- function LateUpdate()
- {
- var dt = Time.deltaTime;
- x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
- y += Input.GetAxis("Vertical") * verticalSpeed * dt;
- y = ClampAngle(y, minVertical, maxVertical);
- var rotation = Quaternion.Euler(y, x, 0);
- var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
- //print(rotation + " && " + rotation * Vector3(0.0, 0.0, -distance));
- transform.rotation = rotation;
- transform.position = position;
- }
- static function ClampAngle (angle : float, min : float, max : float) {
- if (angle < -360)
- angle += 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp (angle, min, max);
- }
- var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
以上是关于Unity3D Quaternion各属性和函数測试的主要内容,如果未能解决你的问题,请参考以下文章
LookRotation用法详解 Unity3d Quaternion.LookRotation实现原理
Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会
csharp Barrel Roll的脚本...解释如何使用Unity3d的Quaternion.Slerp(使用Coroutine代替)