unity3d Quaternion.LookRotation 控制車載機搶旋轉

設備:htc vive。
實現功能:通過兩個手柄控制機槍旋轉。
實現思路:旋轉機槍的父節點(位置在機槍的旋轉基座上),使父節點Z軸始終指向兩個手柄的全局中心點。
代碼:
usingUnityEngine;
usingSystem.Collections;
publicclassHeavyMechineGunRotate:MonoBehaviour{
   publicTransformm_target;//用於顯示兩個手柄的中心點,可去除
   publicSteamVR_TrackedObject[] m_trackedObject;//兩個手柄的位置
   // Use this for initialization
   voidStart()
    {
    }
   // Update is called once per frame
   voidUpdate()
    {
        SetTargetRotate();
        transform.rotation =Quaternion.LookRotation(m_target.position - transform.position);
    }
   publicvoidSetTargetRotate() {
       if(m_trackedObject !=null&& m_trackedObject.Length == 2) {
            m_target.transform.position = (m_trackedObject[0].transform.position + m_trackedObject[1].transform.position) * 0.5f;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章