使用SteamVR实现与物体的交互

8 篇文章 2 订阅
6 篇文章 0 订阅
使用SteamVR实现与物体的交互需要考虑如下问题:

    如何获得按钮事件?如何获得按钮传递的数据?抓取的基本原理(Collider,Rigibody)以及手柄震动的实现

使用SteamVR实现与物体的交互实现步骤
  1. 获取手柄引用
  2. 手柄与Box的碰撞检测
  3. 获取按钮事件
  4. 抓取:Box作为手柄的transform的子物体,失去rigibody相关属性
  5. 松开:Box的parent为空,重新获取自身的rigibody相关属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InterationWithBox : MonoBehaviour {
    private SteamVR_TrackedObject trackedObject;
    private SteamVR_Controller.Device device;
    private GameObject interactBox;
    // Use this for initialization
    void Start ()
    {
        trackedObject = GetComponent<SteamVR_TrackedObject>();
        device = SteamVR_Controller.Input((int)trackedObject.index);
    }

    // Update is called once per frame
    void Update ()
    {
        if (device==null)
        {
            return;
        }
        if (device.GetAxis().x!=0||device.GetAxis().y!=0)
        {
            Debug.Log(device.GetAxis().x+"|"+device.GetAxis().y);
        }
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            if (interactBox!=null)
            {
                interactBox.transform.parent = transform;
                Rigidbody rig = interactBox.GetComponent<Rigidbody>();
                rig.useGravity = false;
                rig.isKinematic = true;
            }
        }
        if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
        {
            interactBox.transform.parent = null;
            Rigidbody rig = interactBox.GetComponent<Rigidbody>();
            rig.useGravity = true;
            rig.isKinematic = false;
        }
    }
    /// <summary>
    /// 碰撞体进入处理函数
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("TriggerEnter");
        interactBox = other.transform.gameObject;
    }
    /// <summary>
    /// 碰撞体脱离处理函数
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerExit(Collider other)
    {
        Debug.Log("TriggerExit");
        interactBox = null;
    }

    private void OnTriggerStay(Collider other)
    {
        if (device != null)
        {
            device.TriggerHapticPulse(800);
        }
    }
}
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的SteamVR 3D物体交互代码示例,使用了Unity和SteamVR插件: ```csharp using UnityEngine; using System.Collections; using Valve.VR; public class ObjectInteraction : MonoBehaviour { public SteamVR_Input_Sources handType; // 左手或右手 public SteamVR_Action_Boolean grabAction; // 抓取动作 public SteamVR_Action_Boolean throwAction; // 抛出动作 private GameObject collidingObject; // 当前碰撞的物体 private GameObject objectInHand; // 当前手中的物体 // 当物体进入碰撞器时,将其设置为当前碰撞的物体 private void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Interactable")) { collidingObject = other.gameObject; } } // 当物体离开碰撞器时,将当前碰撞的物体设为空 private void OnTriggerExit(Collider other) { if (other.gameObject.CompareTag("Interactable")) { collidingObject = null; } } private void Update() { // 抓取物体 if (grabAction.GetLastStateDown(handType)) { if (collidingObject) { GrabObject(); } } // 抛出物体 if (throwAction.GetLastStateDown(handType)) { if (objectInHand) { ReleaseObject(); } } } // 抓取物体 private void GrabObject() { objectInHand = collidingObject; collidingObject = null; var joint = AddFixedJoint(); joint.connectedBody = objectInHand.GetComponent<Rigidbody>(); } // 添加FixedJoint组件 private FixedJoint AddFixedJoint() { FixedJoint fx = gameObject.AddComponent<FixedJoint>(); fx.breakForce = 20000; fx.breakTorque = 20000; return fx; } // 释放物体 private void ReleaseObject() { if (GetComponent<FixedJoint>()) { GetComponent<FixedJoint>().connectedBody = null; Destroy(GetComponent<FixedJoint>()); objectInHand.GetComponent<Rigidbody>().velocity = SteamVR_Input.GetAction<SteamVR_Action_Vector2>("Squeeze").axis * SteamVR_Input.GetAction<SteamVR_Action_Boolean>("GrabPinch").stateDown * SteamVR_Input.GetAction<SteamVR_Action_Boolean>("GrabGrip").stateDown * 10f; objectInHand.GetComponent<Rigidbody>().angularVelocity = SteamVR_Input.GetAction<SteamVR_Action_Vector2>("Touchpad").axis * SteamVR_Input.GetAction<SteamVR_Action_Boolean>("GrabPinch").stateDown * SteamVR_Input.GetAction<SteamVR_Action_Boolean>("GrabGrip").stateDown * 10f; objectInHand = null; } } } ``` 这段代码假设您已经设置好了SteamVR插件和控制器,将此脚本添加到控制器物体上,将“手类型”设置为左手或右手,将“抓取动作”和“抛出动作”设置为您在控制器上使用的实际手势动作。当手部进入物体的触发器时,它将被标记为“当前碰撞的物体”,然后您可以使用“抓取动作”将其抓起,并使用“抛出动作”将其释放。当您释放物体时,它将获得一些速度和角速度,以模拟您实际释放物体时的动作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值