UI跟随相机视角旋转

将脚本挂载到UI上:

// Licensed under the MIT License. See LICENSE in the project root for license information.

using UnityEngine;

namespace HoloToolkit.Unity
{
    //枢轴
    public enum PivotAxis
    {
        // Rotate about all axes.
        //对所有轴旋转。
        Free,
        // Rotate about an individual axis.
        //绕一个轴旋转。
        Y
    }

    /// <summary>
    /// The Billboard class implements the behaviors needed to keep a GameObject oriented towards the user.
    /// 广告牌类实现所需的行为保持GameObject面向用户。
    /// </summary>
    public class Billboard : MonoBehaviour
    {
        /// <summary>
        /// The axis about which the object will rotate.
        /// 关于对象将轴旋转。
        /// </summary>
        /// 指定对象将旋转的轴。
        [Tooltip("Specifies the axis about which the object will rotate.")]
        public PivotAxis PivotAxis = PivotAxis.Free;
        //指定了目标我们将东方。如果没有指定目标的主要将使用相机。
        [Tooltip("Specifies the target we will orient to. If no Target is specified the main camera will be used.")]
        //目标转换
        public Transform TargetTransform;

        private void OnEnable()
        {
            if (TargetTransform == null)
            {
                TargetTransform = Camera.main.transform;
            }

            Update();
        }

        /// <summary>
        /// Keeps the object facing the camera.
        /// 保持相机所面临的对象。
        /// </summary>
        private void Update()
        {
            if (TargetTransform == null)
            {
                return;
            }

            // Get a Vector that points from the target to the main camera.
            //得到一个向量点从目标到主相机。
            Vector3 directionToTarget = TargetTransform.position - transform.position;

            // Adjust for the pivot axis.
            //枢轴的调整。
            switch (PivotAxis)
            {
                case PivotAxis.Y:
                    directionToTarget.y = 0.0f;
                    break;

                case PivotAxis.Free:
                default:
                    // No changes needed.
                    break;
            }

            // If we are right next to the camera the rotation is undefined. 
            //如果我们旁边相机旋转是未定义的
            if (directionToTarget.sqrMagnitude < 0.001f)
            {
                return;
            }

            // Calculate and apply the rotation required to reorient the object
            //计算和应用所需的旋转调整对象
            transform.rotation = Quaternion.LookRotation(-directionToTarget);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,634评论 5 6
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,939评论 0 23
  • 絮在飘,花初绽,草微摇 暗生情,不思量,自难忘 隐心意,寄彩笺,恨长天 琴轻抚,曲未阑,弦已断 泪成珠,落两行,神...
    蒋伊人阅读 486评论 1 3
  • 建表的语法create table 表名(字段1 数据类型 列的特征,字段2 数据类型 列的特征,...) 列的特...
    lianzhanshu阅读 785评论 0 51
  • 家好!我是广西桂林兴安的郑小香,今年45岁,1米58的我有一百四十多重!从小我就微胖,对自己的身材一直都不满意,所...
    康嘉奇营养师一郑小香阅读 407评论 0 0

友情链接更多精彩内容