当前位置:网站首页>[Switch]SteamVR 1.x️ 1. Realize hand-object interaction—based on [CameraRig]

[Switch]SteamVR 1.x️ 1. Realize hand-object interaction—based on [CameraRig]

2023-01-19 15:32:41little girl big

目录

* 实现目标:

* 实现步骤


* 实现目标:

When the handle touches the box,扣动扳机键,拿起盒子(Cube).松开扳机键,放下盒子

a、Get handle reference

b、手柄与Box的碰撞检测

c、获取按钮事件

d、抓取:Boxas a handletransform的子物体,失去rigibody相关属性

e、松开:Box的parent为空,regain itselfrigibody相关属性

* 实现步骤

1、导入SteamVR SDK

2、Delete the originalCamera,拖入[CameraRig]、Create a new box、地面

Adjustable head size:[CameraRig]——SteamVR_PlayArea——Size

3、Get handle reference

The script hangs on the right hand,即Controller (right)

    private SteamVR_TrackedObject trackedObject;
    private SteamVR_Controller.Device device;
	// Use this for initialization
	void Start () {
        trackedObject = GetComponent<SteamVR_TrackedObject>();
        device = SteamVR_Controller.Input((int)trackedObject.index);
	}

4、手柄与Box的碰撞检测

给两个Controller添加Sphere Collider,Colliderand scaled down slightly to fit,勾选Is Trigger,给盒子添加Rigidbody

    private void OnTriggerEnter(Collider other){ }
    private void OnTriggerExit(Collider other){ }

5、获取按钮事件

	void Update () {
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)){ }
        if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)){ }
    }

6、手柄震动

device.TriggerHapticPulse(700);

7、Implementation of grab and let go events

改变cubeThe parent object of the object,改变cubeWhether to use gravity and dynamicsIs Kinematic

Is Kinematic:是否开启动力学,If enabled, the game is no longer affected by the physics engine,只受transform影响

全代码展示:

using UnityEngine;
public class MyController: MonoBehaviour {
    SteamVR_TrackedObject trackedObject;
    SteamVR_Controller.Device device;
    //Get what you collided withbox
    GameObject interactBox;
    // Use this for initialization
    void Start()
    {
        trackedObject = GetComponent<SteamVR_TrackedObject>();
        device = SteamVR_Controller.Input((int)trackedObject.index);
    }
    void Update()
    {
        //按下Trigger键
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            if (interactBox != null)
            {
                interactBox.transform.parent = transform;
                interactBox.GetComponent<Rigidbody>().useGravity = false;
                interactBox.GetComponent<Rigidbody>().isKinematic = true;
            }
        }
        //松开Trigger键
        if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
        {
            if (interactBox != null)
            {
                interactBox.transform.parent = transform.parent.parent;
                interactBox.GetComponent<Rigidbody>().useGravity = true;
                interactBox.GetComponent<Rigidbody>().isKinematic = false;
                interactBox = null;
            }
        }
    }
    void OnTriggerEnter(Collider other)
    {
        interactBox = other.transform.gameObject;
    }
    void OnTriggerExit(Collider other)
    {
        interactBox = null;
    }
    void OnTriggerStay(Collider other)
    {
        if (device != null)
        {
            //触发震动
            device.TriggerHapticPulse(700);
        }
    }
}

原网站

版权声明
本文为[little girl big]所创,转载请带上原文链接,感谢
https://chowdera.com/2023/019/202301191523582385.html

随机推荐