vrtk 手柄的移动方向判断

安住手柄的trigger键,然后移动手柄,判断是从左到右,从右到左,从上到下,从下到上

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TranslateAndRotateCtrl : MonoBehaviour {
    Vector3 first = Vector3.zero;    //记录鼠标点击的初始位置    
    Vector3 second = Vector3.zero;   //记录鼠标移动时的位置    


    /// <summary>
    /// 最低高度
    /// </summary>
    public float LimitMinY = -2.8f;


    /// <summary>
    /// 最高高度
    /// </summary>
    public float LimitMaxY = 8f;

    bool dragging = false;   //标记是否鼠标在滑动    


    [SerializeField]
    SteamVR_TrackedObject trackedObj;

    [Tooltip("设置旋转角度,默认为100")]
    public float setRotateAngle = 150.0f;
    [Tooltip("设置上下移动距离,默认为0.5")]
    public float setMoveDistance = 2f;

    public Transform rotatorObj;
    public Transform translateObj;

    public bool isOpenRotate = true;
     bool isChangeUpAndDown = true;
    private void Start()
    {

    }

    void Update()
    {
        var device = SteamVR_Controller.Input((int)trackedObj.index);
        if (device == null) return;

        //if (decive.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
        //{   //记录鼠标按下的位置      

        //    MainUi.Instance.ShowMenu();

        //}


        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
        {   //记录鼠标按下的位置      

            first = device.transform.pos;
        }

        dragging = false;

        if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
        {   //记录鼠标拖动的位置      

            dragging = true;
            second = device.transform.pos;
            Vector3 slideDirection = second - first;
            float x = slideDirection.x, y = slideDirection.y, z = slideDirection.z;


            if (Mathf.Abs(y) > Mathf.Abs(x) && Mathf.Abs(y) > Mathf.Abs(z))
            {
                //上下
                if (y > 0)
                {
                    // Debug.Log("down");

                    if (isChangeUpAndDown)
                    {
                        translateObj.Translate(Vector3.up * Time.deltaTime * setMoveDistance);
                        if (translateObj.localPosition.y >= LimitMaxY)
                            translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMaxY, translateObj.localPosition.z);
                    }
                    else
                    {
                        translateObj.Translate(-Vector3.up * Time.deltaTime * setMoveDistance);
                        if (translateObj.localPosition.y <= LimitMinY)
                            translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMinY, translateObj.localPosition.z);
                    }


                }
                else
                {
                    // Debug.Log("up");
                    if (isChangeUpAndDown)
                    {
                        translateObj.Translate(-Vector3.up * Time.deltaTime * setMoveDistance);
                        if (translateObj.localPosition.y <= LimitMinY)
                            translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMinY, translateObj.localPosition.z);
                    }
                    else
                    {
                        translateObj.Translate(Vector3.up * Time.deltaTime * setMoveDistance);
                        if (translateObj.localPosition.y >= LimitMaxY)
                            translateObj.localPosition = new Vector3(translateObj.localPosition.x, LimitMaxY, translateObj.localPosition.z);
                    }


                }
            }
            else if (Mathf.Abs(x) > Mathf.Abs(y) || Mathf.Abs(z) > Mathf.Abs(y))
            {
                //左右


                if (!isOpenRotate) return;

                if (Mathf.Abs(x) > Mathf.Abs(z))  //X轴上变化
                {
                    if (x > 0)
                    {
                        Debug.Log("right");
                        rotatorObj.Rotate(-Vector3.up * Time.deltaTime * setRotateAngle);
                    }
                    else
                    {
                        Debug.Log("left");
                        rotatorObj.Rotate(Vector3.up * Time.deltaTime * setRotateAngle);

                    }
                }
                else   //z轴上变化
                {
                    if (z > 0)
                    {
                        Debug.Log("right");
                        rotatorObj.Rotate(-Vector3.up * Time.deltaTime * setRotateAngle);
                    }
                    else
                    {
                        Debug.Log("left");
                        rotatorObj.Rotate(Vector3.up * Time.deltaTime * setRotateAngle);


                    }
                }


            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值