using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using VRTK; namespace ProcedureBYQ { /// /// 移动控制器 /// public class PlayMoveWayController : MonoBehaviour { private float _minSpeed = -1f; private float _maxSpeed = 3; public LayerMask notMoveLayer; public Transform _cameraEye; public Transform maxPoint; public Transform minPoint; private float xMax; private float xMin; private float yMax; private float yMin; private float zMax; private float zMin; private void Start() { SetLimit(); } void Update() { GameObject rightHand = VRTK_DeviceFinder.GetControllerRightHand(); VRTK_ControllerReference rightHandReference = VRTK_ControllerReference.GetControllerReference(rightHand); if (VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Touchpad, SDK_BaseController.ButtonPressTypes.Press, rightHandReference)) { Vector2 axis = VRTK_SDK_Bridge.GetControllerAxis(SDK_BaseController.ButtonTypes.Touchpad, rightHandReference); Vector3 moveDir = rightHand.transform.forward * axis.y * _maxSpeed; Ray ray; if (axis.y > 0) ray = new Ray(_cameraEye.transform.position, rightHand.transform.forward); else ray = new Ray(_cameraEye.transform.position, -rightHand.transform.forward); //射线检测,移动方向遇到遮罩区以外碰撞器,不移动 if (Physics.Raycast(ray, 0.3f, notMoveLayer.value)) return; this.transform.Translate(moveDir * Time.deltaTime, Space.World); LimitMove(); } } private void LimitMove() { transform.position = new Vector3(Mathf.Clamp(transform.position.x, xMin, xMax), Mathf.Clamp(transform.position.y, yMin, yMax), Mathf.Clamp(transform.position.z, zMin, zMax)); } private void SetLimit() { SetCameraPosLimite(minPoint.position.x, maxPoint.position.x, minPoint.position.y, maxPoint.position.y, minPoint.position.z, maxPoint.position.z); } private void SetCameraPosLimite(float _xMin, float _xMax, float _yMin, float _yMax, float _zMin, float _zMax) { xMax = _xMax; xMin = _xMin; yMax = _yMax; yMin = _yMin; zMax = _zMax; zMin = _zMin; } } }