VRTK根据手柄朝向移动

根据手柄的朝向实现位移,有区域限制,碰撞检测

using Sirenix.OdinInspector;
using DG.Tweening;
using UnityEngine;
using VRTK;


public class VRTK_MoveByTouch : MonoBehaviour
{
    [LabelText("移动速度")]
    public float moveSpeed = 10;
    [LabelText("是否可以穿模")]
    public bool isCanCrossCollider = false;
    [LabelText("是否限制范围")]
    public bool isLimitRange = true;

    [BoxGroup("限制范围")]
    [ShowIf("isLimitRange")]
    public float max_X;
    [BoxGroup("限制范围")]
    [ShowIf("isLimitRange")]
    public float min_X;
    [BoxGroup("限制范围")]
    [ShowIf("isLimitRange")]
    public float max_Y;
    [BoxGroup("限制范围")]
    [ShowIf("isLimitRange")]
    public float min_Y;
    [BoxGroup("限制范围")]
    [ShowIf("isLimitRange")]
    public float max_Z;
    [BoxGroup("限制范围")]
    [ShowIf("isLimitRange")]
    public float min_Z;

    private VRTK_Interaction vrtk_Interaction;

    private Vector2 axis;
    private Vector3 direction;
    private bool isCanMove = false;
    private VRTK_ControllerEvents moveHand;
    private Vector2 touchVec = Vector2.zero;

    void Awake()
    {
        vrtk_Interaction = FindObjectOfType<VRTK_Interaction>();
        vrtk_Interaction.TouchpadStart += TouchpadStart;
        vrtk_Interaction.TouchAxisChanged += TouchAxisChanged;
        vrtk_Interaction.TouchpadEnd += TouchpadEnd;
    }

    private void TouchpadStart(object arg1, ControllerInteractionEventArgs args)
    {
        isCanMove = true;
    }

    private void TouchAxisChanged(object sender, ControllerInteractionEventArgs e)
    {
        moveHand = (VRTK_ControllerEvents)sender;

        touchVec = e.touchpadAxis;
    }

    private void TouchpadEnd(object arg1, ControllerInteractionEventArgs args)
    {
        isCanMove = false;
    }

    void Update()
    {
        if (!isCanCrossCollider)
        {
            CheckSphereCollider();
        }

        if (isCanMove)
        {
            Move();
        }
    }

    private void Move()
    {
        if (!moveHand) return;

        axis = moveHand.GetAxis(VRTK_ControllerEvents.Vector2AxisAlias.Touchpad);

        direction = moveHand.transform.forward * axis.y * moveSpeed * Time.deltaTime;

        LimitRange();
    }

    private void LimitRange()
    {
        transform.Translate(direction, Space.World);
        if (isLimitRange)
            transform.position = ClampPosition(transform.position);
    }

    /// 
    /// 确定区域
    /// 
    /// 
    /// 
    private Vector3 ClampPosition(Vector3 pos)
    {
        var x = Mathf.Clamp(pos.x, min_X, max_X);
        var y = Mathf.Clamp(pos.y, min_Y, max_Y);
        var z = Mathf.Clamp(pos.z, min_Z, max_Z);

        return new Vector3(x, y, z);
    }

    /// 
    /// 球形检测
    /// 
    private void CheckSphereCollider()
    {
        if(Physics.CheckSphere(transform.position, 0.5f))
        {
            direction = moveHand.transform.forward * -axis.y * moveSpeed * Time.deltaTime;

            LimitRange();
        }
    }
    public Vector2 GetPos()
    {
        return new Vector2(transform.position.x, transform.position.z);
    }

    public void SetPosLimitY(Vector3 pos)
    {
        transform.position = pos;

        transform.localPosition = new Vector3(transform.localPosition.x, 0, transform.localPosition.z);
    }

    public void SetPosNoLimitY(Vector3 pos, Space space)
    {
        if (space == Space.World)
            transform.DOMove(pos, 2);
        else
            transform.DOLocalMove(pos, 2);
    }
}

你可能感兴趣的:(vr)