Unity之Quaternion学习三

using UnityEngine;
using System.Collections;

public class SinDemo : MonoBehaviour {

    public GameObject gameobj1;
    float dis = 0.0001f;
    bool isTrue = false;
    
    // Use this for initialization
	void Start () {
        
        /*AngleAxis
        //transform沿y轴旋转30度
        transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
        //transform沿z轴旋转30度
        transform.rotation = Quaternion.AngleAxis(30, Vector3.forward);
        //transform沿x轴旋转30度
        transform.rotation = Quaternion.AngleAxis(30, Vector3.left);
         * */

        /*eulerAngles
        Quaternion rotation = Quaternion.identity; // (0.0, 0.0, 0.0, 1.0)
        //y轴旋转30度
        rotation.eulerAngles = new Vector3(0, 30, 0);
        transform.rotation = rotation;
         */

        /*Euler
         //y轴旋转30度
        Quaternion rotation = Quaternion.Euler(0, 30, 0);
        //transform.rotation = rotation;
         //y轴旋转30度
        rotation = Quaternion.Euler(new Vector3(0, 30, 0));
        transform.rotation = rotation;
         * */
        /*FromToRotation
        //从一个方向转到另外一个方向,把y轴指向z轴
        //transform.rotation = Quaternion.FromToRotation(Vector3.up, Vector3.forward);
         */
        /*Inverse:角度取反
        Quaternion rotation = Quaternion.Euler(0, 30, 0);
        transform.rotation = rotation;
        Debug.LogError("normal rotation is " + rotation);
        rotation = Quaternion.Inverse(rotation);
        Debug.LogError("inverse rotation is " + rotation);*/
        //LookRotation
        //计算出gameobj1到transform的方向
        Vector3 relativePos = transform.position - gameobj1.transform.position;
        //物体看向relativePos这个方向
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        gameobj1.transform.rotation = rotation;

        Debug.LogError("rotation is " + rotation);
    }



    /// 
    /// 如果你想绘制可被点选的gizmos,执行这个函数
    /// 
    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Vector3 direction = Vector3.right * 2;  //世界坐标系的 轴向x
        Gizmos.DrawRay(transform.position, direction);
        Gizmos.color = Color.green;
        direction = Vector3.up * 2;   //世界坐标系的 轴向y
        Gizmos.DrawRay(transform.position, direction);
        Gizmos.color = Color.blue;
        direction = Vector3.forward * 2;     //世界坐标系的 轴向z
        Gizmos.DrawRay(transform.position, direction);
    }

    Quaternion fromQua = Quaternion.Euler(0, 10, 0);
    Quaternion toQua = Quaternion.Euler(0, 80, 0);
    float speed = 0.1f;
	void Update () {
        //Quaternion.Slerp:fromQua旋转角度变换到toQua角度
        //transform.rotation = Quaternion.Slerp(fromQua, toQua, Time.time * speed);
         transform.rotation = Quaternion.Lerp(fromQua, toQua, Time.time * speed);
    }

你可能感兴趣的:(游戏开发语言之Unity3D)