vrtk:ui面板朝向摄像机

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luoyikun/article/details/88026428
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的设置为
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/luoyikun/article/details/88026428