1. 程式人生 > >四元數旋轉

四元數旋轉

using UnityEngine;
using System.Collections;

public class SetFromToDirection_ts : MonoBehaviour
{

    public Transform A, B;
    float angle;
    Vector3 axis = Vector3.zero;
    float xSpeed = 0.0f, ySpeed = 0.0f, zSpeed = 0.0f;
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        xSpeed += 0.5f * Time.deltaTime;
        ySpeed += 1.0f * Time.deltaTime;
        zSpeed += 2.5f * Time.deltaTime;
        //直接賦值
        A.eulerAngles = new Vector3(xSpeed, ySpeed, zSpeed);

        //建立例項
        Quaternion q=Quaternion.identity;
        q.eulerAngles= new Vector3(xSpeed, ySpeed, zSpeed);
        B.rotation = q;

        //獲取A的rotation的旋轉軸和角度
        A.rotation.ToAngleAxis(out angle, out axis);
        //設定B的rotation,使得B的rotation和A相同
         B.rotation = Quaternion.AngleAxis(angle, axis);
    }
}