1. 程式人生 > >SteamVR動態載入後固定視角

SteamVR動態載入後固定視角

public class Test : MonoBehaviour
{
    public GameObject PrefabObj;  //預設
    public Transform Cube1;   //載入後眼睛面向的方向和位置
    Transform m_head;  //頭部相機
    GameObject m_CmaeraRig;  //載入後的物件

    void Start()
    {
        m_CmaeraRig = Instantiate(PrefabObj) as GameObject;
        m_head = m_CmaeraRig.transform.FindChild("Camera (head)");
        StartCoroutine(SetTransform());
    }

    float f;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SetCameraRigTransform();
        }
    }

    IEnumerator SetTransform()
    {
        //載入進來之後需要給一段時間讓它初始化
        yield return new WaitForSeconds(0.03f); 
        SetCameraRigTransform();
    }

    /// <summary>
    /// 設定CameraRig的變換
    /// </summary>
    void SetCameraRigTransform()
    {
        m_CmaeraRig.transform.position = Cube1.position;
        f = Cube1.eulerAngles.y - m_head.localEulerAngles.y;
        if (f > 360) f -= 360;
        if (f < 0) f += 360;
        m_CmaeraRig.transform.rotation = Quaternion.Euler(new Vector3(Cube1.eulerAngles.x, f, Cube1.eulerAngles.z));
    }
}