using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuaternionAPI : MonoBehaviour
{
public Transform tf;
public AnimationCurve a;
private float x;
四元数*向量:表示对向量做旋转变化
//2D视图3D物体前方朝向鼠标位置
private void Rotext()
{
Vector3 fdir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotY = Mathf.Atan2(fdir.x, fdir.y) * Mathf.Rad2Deg;
//采用四元数设置旋转角度:先在世界x轴旋转-90的在*得加世界z轴目标旋转值
transform.rotation = Quaternion.AngleAxis(-90, Vector3.right) * Quaternion.AngleAxis(-rotY, Vector3.up);
}
private void OnGUI()
{
if (GUILayout.Button (""))
{
//1.欧拉角转换为四元数
//Quaternion.Euler(欧拉角);
//2/四元数转欧拉角
Quaternion qt = this.transform.rotation;
Vector3 auler = qt.eulerAngles;
//3.轴/角
//Quaternion.Euler(0, 50, 0);
this .transform .rotation = Quaternion.AngleAxis(50, Vector3.up);
}
if (GUILayout.Button("LookRotation"))
{
//4.注视旋转
//Z轴指向一个方向
//当前物体注视 tf 旋转
Vector3 dir = tf.position - this.transform.position;
this.transform.rotation = Quaternion.LookRotation (dir);
//this.transform.LookAt(tf);//效果与上两行代码相同----下一帧立即执行
}
if (GUILayout.RepeatButton("LookRotationLerp"))
{
//5.插值旋转
x += Time.deltaTime;
Quaternion dir = Quaternion.LookRotation(tf.position - this.transform.position);
this .transform .rotation = Quaternion.Lerp(this.transform.rotation, dir, a.Evaluate (x));
//6.匀速旋转
//this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, dir, 0.1f);//匀速朝向旋转
}
if (GUILayout.RepeatButton("AngleR90"))
{
Quaternion dir = Quaternion.Euler(0, 90, 0);
this.transform.rotation =
Quaternion.Lerp(this.transform.rotation, dir, 0.1f);
//7.如果当前旋转角度 接近目标旋转角度
if (Quaternion .Angle (this .transform .rotation ,dir )<10)
{
this.transform.rotation = dir;
}
}
if (GUILayout.RepeatButton("Right"))
{
//8.x轴注视旋转 t1
//this.transform.right = tf.position - this.transform.position;
//x正方向---注视目标物体方向
Quaternion dir = Quaternion.FromToRotation(Vector3 .right , tf.position - this.transform.position);
//this.transform.rotation = dir;
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, dir, 0.1f);
}
}
// Update is called once per frame
void Update()
{
}
}