修改了一下Teleport和TeleportPoint两个脚本添加了一个MoveTarget选项移动指定目标点

//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: Single location that the player can teleport to
//
//=============================================================================

using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Valve.VR.InteractionSystem
{
    //-------------------------------------------------------------------------
    public class TeleportPoint : TeleportMarkerBase
    {
        public enum TeleportPointType
        {
            MoveToLocation,
            SwitchToNewScene,
            MoveTarget
        };

        //Public variables
        public TeleportPointType teleportType = TeleportPointType.MoveToLocation;
        public string title;
        public string switchToScene;
        public Color titleVisibleColor;
        public Color titleHighlightedColor;
        public Color titleLockedColor;
        public bool playerSpawnPoint = false;

        public Transform Target;
        //Private data
        private bool gotReleventComponents = false;
        private MeshRenderer markerMesh;
        private MeshRenderer switchSceneIcon;
        private MeshRenderer moveLocationIcon;
        private MeshRenderer lockedIcon;
        private MeshRenderer pointIcon;
        private Transform lookAtJointTransform;
        private new Animation animation;
        private Text titleText;
        private Player player;
        private Vector3 lookAtPosition = Vector3.zero;
        private int tintColorID = 0;
        private Color tintColor = Color.clear;
        private Color titleColor = Color.clear;
        private float fullTitleAlpha = 0.0f;

        //Constants
        private const string switchSceneAnimation = "switch_scenes_idle";
        private const string moveLocationAnimation = "move_location_idle";
        private const string lockedAnimation = "locked_idle";


        //-------------------------------------------------
        public override bool showReticle
        {
            get
            {
                return false;
            }
        }


        //-------------------------------------------------
        void Awake()
        {
            GetRelevantComponents();

            animation = GetComponent<Animation>();

            tintColorID = Shader.PropertyToID("_TintColor");

            moveLocationIcon.gameObject.SetActive(false);
            switchSceneIcon.gameObject.SetActive(false);
            lockedIcon.gameObject.SetActive(false);

            UpdateVisuals();
        }


        //-------------------------------------------------
        void Start()
        {
            player = Player.instance;
        }


        //-------------------------------------------------
        void Update()
        {
            if (Application.isPlaying)
            {
                lookAtPosition.x = player.hmdTransform.position.x;
                lookAtPosition.y = lookAtJointTransform.position.y;
                lookAtPosition.z = player.hmdTransform.position.z;

                lookAtJointTransform.LookAt(lookAtPosition);
            }
        }


        //-------------------------------------------------
        public override bool ShouldActivate(Vector3 playerPosition)
        {
            return (Vector3.Distance(transform.position, playerPosition) > 1f);
        }


        //-------------------------------------------------
        public override bool ShouldMovePlayer()
        {
            return true;
        }


        //-------------------------------------------------
        public override void Highlight(bool highlight)
        {
            if (!locked)
            {
                if (highlight)
                {
                    SetMeshMaterials(Teleport.instance.pointHighlightedMaterial, titleHighlightedColor);
                }
                else
                {
                    SetMeshMaterials(Teleport.instance.pointVisibleMaterial, titleVisibleColor);
                }
            }

            if (highlight)
            {
                pointIcon.gameObject.SetActive(true);
                animation.Play();
            }
            else
            {
                pointIcon.gameObject.SetActive(false);
                animation.Stop();
            }
        }


        //-------------------------------------------------
        public override void UpdateVisuals()
        {
            if (!gotReleventComponents)
            {
                return;
            }

            if (locked)
            {
                SetMeshMaterials(Teleport.instance.pointLockedMaterial, titleLockedColor);

                pointIcon = lockedIcon;

                animation.clip = animation.GetClip(lockedAnimation);
            }
            else
            {
                SetMeshMaterials(Teleport.instance.pointVisibleMaterial, titleVisibleColor);

                switch (teleportType)
                {
                    case TeleportPointType.MoveToLocation:
                        {
                            pointIcon = moveLocationIcon;

                            animation.clip = animation.GetClip(moveLocationAnimation);
                        }
                        break;
                    case TeleportPointType.SwitchToNewScene:
                        {
                            pointIcon = switchSceneIcon;

                            animation.clip = animation.GetClip(switchSceneAnimation);
                        }
                        break;
                    case TeleportPointType.MoveTarget:
                        {
                            pointIcon = moveLocationIcon;

                            animation.clip = animation.GetClip(moveLocationAnimation);
                        }
                        break;
                }
            }

            titleText.text = title;
        }


        //-------------------------------------------------
        public override void SetAlpha(float tintAlpha, float alphaPercent)
        {
            tintColor = markerMesh.material.GetColor(tintColorID);
            tintColor.a = tintAlpha;

            markerMesh.material.SetColor(tintColorID, tintColor);
            switchSceneIcon.material.SetColor(tintColorID, tintColor);
            moveLocationIcon.material.SetColor(tintColorID, tintColor);
            lockedIcon.material.SetColor(tintColorID, tintColor);

            titleColor.a = fullTitleAlpha * alphaPercent;
            titleText.color = titleColor;
        }


        //-------------------------------------------------
        public void SetMeshMaterials(Material material, Color textColor)
        {
            markerMesh.material = material;
            switchSceneIcon.material = material;
            moveLocationIcon.material = material;
            lockedIcon.material = material;

            titleColor = textColor;
            fullTitleAlpha = textColor.a;
            titleText.color = titleColor;
        }


        //-------------------------------------------------
        public void TeleportToScene()
        {
            if (!string.IsNullOrEmpty(switchToScene))
            {
                Debug.Log("<b>[SteamVR Interaction]</b> TeleportPoint: Hook up your level loading logic to switch to new scene: " + switchToScene);
            }
            else
            {
                Debug.LogError("<b>[SteamVR Interaction]</b> TeleportPoint: Invalid scene name to switch to: " + switchToScene);
            }
        }


        //-------------------------------------------------
        public void GetRelevantComponents()
        {
            markerMesh = transform.Find("teleport_marker_mesh").GetComponent<MeshRenderer>();
            switchSceneIcon = transform.Find("teleport_marker_lookat_joint/teleport_marker_icons/switch_scenes_icon").GetComponent<MeshRenderer>();
            moveLocationIcon = transform.Find("teleport_marker_lookat_joint/teleport_marker_icons/move_location_icon").GetComponent<MeshRenderer>();
            lockedIcon = transform.Find("teleport_marker_lookat_joint/teleport_marker_icons/locked_icon").GetComponent<MeshRenderer>();
            lookAtJointTransform = transform.Find("teleport_marker_lookat_joint");

            titleText = transform.Find("teleport_marker_lookat_joint/teleport_marker_canvas/teleport_marker_canvas_text").GetComponent<Text>();

            gotReleventComponents = true;
        }


        //-------------------------------------------------
        public void ReleaseRelevantComponents()
        {
            markerMesh = null;
            switchSceneIcon = null;
            moveLocationIcon = null;
            lockedIcon = null;
            lookAtJointTransform = null;
            titleText = null;
        }


        //-------------------------------------------------
        public void UpdateVisualsInEditor()
        {
            if (Application.isPlaying)
            {
                return;
            }

            GetRelevantComponents();

            if (locked)
            {
                lockedIcon.gameObject.SetActive(true);
                moveLocationIcon.gameObject.SetActive(false);
                switchSceneIcon.gameObject.SetActive(false);

                markerMesh.sharedMaterial = Teleport.instance.pointLockedMaterial;
                lockedIcon.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值