unity VR 对象的旋转平移缩放

操作说明:因涉及到触发对象后操作,所以旋转和缩放需要通过扳机键切换来使用。通过按住拾取键左手柄平移,右手柄旋转,当点击右手柄扳机键后切换为缩放,此时旋转功能将无法使用,左右手柄均可平移,双手柄操作为缩放。再点击右手柄扳机键,右手柄可恢复旋转功能。

一、平移缩放

1、新建一个球形触发器作为操作对象,需要操作的模型可以作为此触发球的子对象,自行调节触发范围。

2、平移使用抓取模型的功能即可。下图小箭头,是默认左手柄触发平移。

3、VRTK_AxisScaleGrabAction 为VRTK缩放对象脚本

二、旋转

update函数写的是通过获取手柄的位置来对模型对象进行旋转控制。

后面三个都是手柄控制器按键事件,最后一个是扳机键按下用来切换旋转和缩放功能的。


    Vector3 previousPosition;
    Vector3 offset;
    bool isRotate = true;
    private bool isOpertion = false;

    void Update()
    {
        //抓取键 是否进行操作
        if (!isOpertion)
        {
            previousPosition = controllerRight.position;
        }else
        {
            //通过手柄位置来控制模型的旋转
            offset = controllerRight.position - previousPosition;
            previousPosition = controllerRight.position;

            float xdis = Mathf.Abs(offset.x);
            float ydis = Mathf.Abs(offset.y);
            if (xdis > ydis)
            {
                if (offset.x > 0)
                {
                    sphereMoveObj.transform.Rotate(Vector3.down, offset.magnitude * 80f, Space.World);
                }

                if (offset.x < 0)
                {
                    sphereMoveObj.transform.Rotate(Vector3.up, offset.magnitude * 80f, Space.World);
                }
            }
            else
            {
                if (offset.y > 0)
                {
                    sphereMoveObj.transform.Rotate(Vector3.right , offset.magnitude * 80f, Space.World);
                }

                if (offset.y < 0)
                {
                    sphereMoveObj.transform.Rotate(Vector3.left, offset.magnitude * 80f, Space.World);
                }
            }

        }
    }




    //右手柄释放抓取按钮时发出
    private void VrtkIG_Right_GrabButtonReleased(object sender, ControllerInteractionEventArgs e)
    {
        isOpertion = false;

    }
    //右手柄在抓取有效对象时发出
    private void VrtkIG_Right_ControllerGrabInteractableObject(object sender, ObjectInteractEventArgs e)
    {
        if (isRotate)
        {
            isOpertion = true;
        }
    }

    //当触发器受到少量挤压时发出
    private void VrtkCE_Right_TriggerTouchStart(object sender, ControllerInteractionEventArgs e)
    {
        
        //切换旋转和平移
        if (sphereMoveObj.transform.GetComponent<VRTK_InteractableObject>().allowedGrabControllers == VRTK_InteractableObject.AllowedController.Both)
        {
            sphereMoveObj.transform.GetComponent<VRTK_InteractableObject>().allowedGrabControllers = VRTK_InteractableObject.AllowedController.LeftOnly;
            isRotate = true;
        }
        else
        {
            sphereMoveObj.transform.GetComponent<VRTK_InteractableObject>().allowedGrabControllers = VRTK_InteractableObject.AllowedController.Both;
            isRotate = false;
        }
     }

 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值