unity vr 数据手套接入

本文分享了如何在VR项目中使用诺依藤手套替代手柄,解决触发事件、UI交互、物体互动和场景移动问题。通过手势识别、射线交互和碰撞检测实现手套操作的转换,适用于其他开发者参考.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言:最近搞一个vr项目,同时又需要接入数据手套.这个手套的牌子是诺依藤的.经过一段时间的折腾.终于将项目遇到的问题一一解决了.后面有其他小伙伴搞类似的工程的时候.可以对你有所帮助.

vr项目接入手套,将手柄的功能用手套取而代之,需要解决以下问题:1:手套代替手柄触发事件2:手柄触发的事件和UI交互3:手柄触发的事件和场景物体交互4:手柄触发事件是自身能在场景移动.

解决思路

1:手套代替手柄触发事件,我是通过手势来判断.将中指,拇指,掌心出建立一个小球.通过中指,拇指本别与掌心的碰撞来触发事件.

        1.1左手拇指与掌心触发自身位移移动事件.

        1.2右手拇指与掌心触发UI事件

        1.3 右手中指与掌心触发与场景交互事件

2:右手拇指与掌心贴合,发出射线,松开时触发UI事件

	private void OnTriggerStay(Collider col)
	{
		if (col.gameObject.layer != LayerMask.NameToLayer("Ignore Raycast"))
		{
            //拇指与中指贴合
			if(muzhi_controllui &&col.gameObject == muzhi_controllui.gameObject && interactionSys)
            {
				interactionSys.M_UIClick_onState();
			}
		}
	}
	private void OnTriggerExit(Collider col)
	{
		if (col.gameObject.layer != LayerMask.NameToLayer("Ignore Raycast"))
		{
            //拇指与掌心分开
			if (muzhi_controllui && col.gameObject == muzhi_controllui.gameObject && interactionSys)
			{
				interactionSys.M_UIClick_onStateUp();
			}
		}
	}

 射线和UI交互的部分当时我参考的是这位大神的文章https://blog.youkuaiyun.com/int_lin/article/details/111591340

3:右手中指与掌心贴合时,触发事件,此时检测手是否触发场景的物体.如果检测到中指与掌心分离时则触发相应的事件.

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

public class VR_ShouTaoZhongZhi : MonoBehaviour
{
    public GameObject xiao_zhi;
    public RightController rightController;
    public LeftController leftController;
    private Transform trans { get; set; }
    private void Start()
    {
        trans = transform;
    }
    private void Update()
    {
        trans.localPosition = Vector3.zero;
    }
    //public bool IsZhuaWo { get; private set; }
    private void OnTriggerEnter(Collider col)
    {
        if (col.gameObject == xiao_zhi)
        {
            //开始抓握
            Debug.LogError("---------------------  抓握::");
        }

        if (rightController)
        {
            rightController.ZhuaWo(true);
        }
        if (leftController)
        {
            leftController.ZhuaWo(true);
        }
        if (FollowMouse.instance)
        {
            FollowMouse.instance.LeftPressDown();
        }
    }
    private void OnTriggerStay(Collider col)
    {
        //if (col.gameObject == xiao_zhi)
        //{
        //    IsZhuaWo = true;
        //    Debug.LogError("-------------------------  抓握::" + IsZhuaWo);
        //}
    }
    private void OnTriggerExit(Collider col)
    {
        //if (col.gameObject == xiao_zhi)
        //{
        //    IsZhuaWo = false;
        //    Debug.LogError("-------------------------  抓握::" + IsZhuaWo);
        //}
        if (rightController)
        {
            rightController.ZhuaWo(false);
        }
        if(leftController)
        {
            leftController.ZhuaWo(false);
        }
        if (FollowMouse.instance)
        {
            FollowMouse.instance.LeftPressUp();
        }
    }
}

4:左手拇指与掌心贴合时,向地面发出射线,松开时,射线与地面交互的点就是要瞬移的地方.



using System;
using UnityEngine;

public class LaserPointer : MonoBehaviour
{
    public Transform cameraRigTransform;
    public Transform headTransform; // The camera rig's head
    public Vector3 teleportReticleOffset; // Offset from the floor for the reticle to avoid z-fighting
    public LayerMask teleportMask; // Mask to filter out areas where teleports are allowed

    private SteamVR_TrackedObject trackedObj;

    public GameObject laserPrefab; // The laser prefab
    private GameObject laser; // A reference to the spawned laser
    private Transform laserTransform; // The transform component of the laser for ease of use

    public GameObject teleportReticlePrefab; // Stores a reference to the teleport reticle prefab.
    private GameObject reticle; // A reference to an instance of the reticle
    private Transform teleportReticleTransform; // Stores a reference to the teleport reticle transform for ease of use

    private Vector3 hitPoint; // Point where the raycast hits
    private bool shouldTeleport; // True if there's a valid teleport target

    private SteamVR_Controller.Device Controller
    {
        get { return SteamVR_Controller.Input((int)trackedObj.index); }
    }

    void Awake()
    {
        trackedObj = GetComponent<SteamVR_TrackedObject>();
    }

    //new
    void Start()
    {
        laser = Instantiate(laserPrefab);
        laserTransform = laser.transform;
        reticle = Instantiate(teleportReticlePrefab);
        teleportReticleTransform = reticle.transform;
    }

    private void ShowLaser(RaycastHit hit)
    {
        if(!laser)
        {
            Start();
        }
        laser.SetActive(true); //Show the laser
        laserTransform.position = Vector3.Lerp(trackedObj.transform.position, hitPoint, .5f); // Move laser to the middle between the controller and the position the raycast hit
        laserTransform.LookAt(hitPoint); // Rotate laser facing the hit point
        laserTransform.localScale = new Vector3(laserTransform.localScale.x, laserTransform.localScale.y,
            hit.distance); // Scale laser so it fits exactly between the controller & the hit point
    }

    private void Teleport()
    {
        shouldTeleport = false; // Teleport in progress, no need to do it again until the next touchpad release
        reticle.SetActive(false); // Hide reticle
        Vector3 difference = cameraRigTransform.position - headTransform.position; // Calculate the difference between the center of the virtual room & the player's head
        difference.y = 0; // Don't change the final position's y position, it should always be equal to that of the hit point

        cameraRigTransform.position = hitPoint + difference; // Change the camera rig position to where the the teleport reticle was. Also add the difference so the new virtual room position is relative to the player position, allowing the player's new position to be exactly where they pointed. (see illustration)
    }

    internal void ShunYiUpdate(bool show_logo)
    {
        if (show_logo)
        {
            RaycastHit hit;

            // Send out a raycast from the controller
            if (Physics.Raycast(trackedObj.transform.position, transform.forward, out hit, 100, teleportMask))
            {
                hitPoint = hit.point;

                ShowLaser(hit);

                //Show teleport reticle
               
                reticle.SetActive(true);
                teleportReticleTransform.position = hitPoint + teleportReticleOffset;

                shouldTeleport = true;
                Debug.LogError(reticle.transform.name+"   "+reticle.gameObject.activeSelf);
            }
        }
        else
        {
            laser.SetActive(false);
            reticle.SetActive(false);

            Teleport();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值