vrtk:ui面板朝向攝像機

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Framework.Tools
{
    public class FollowCamera : MonoBehaviour
    {
        private Transform mTrans;
        private Transform mMainCameraTrans;

        // Use this for initialization
        void Start()
        {
            mTrans = transform;
            Camera tempC = Camera.main;
            if (tempC != null)
            {
                mMainCameraTrans = tempC.transform;
            }
        }

        // Update is called once per frame
        void Update()
        {
            if (mMainCameraTrans == null)
            {
                Camera tempC = Camera.main;
                if (tempC == null)
                {
                    return;
                }
                mMainCameraTrans = tempC.transform;
            }

            Vector3 relativePos = mTrans.position - mMainCameraTrans.position;
            Quaternion lookAtRotation = Quaternion.LookRotation(relativePos, Vector3.up);
            mTrans.rotation = Quaternion.Lerp(mTrans.rotation, lookAtRotation, 1.5f * Time.deltaTime);
        }
    }
}

注意要把場景中的maincamera刪除
canvas的設置爲
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章