IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> unity vr 数据手套接入 -> 正文阅读

[游戏开发]unity vr 数据手套接入

引言:最近搞一个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.csdn.net/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();
        }
    }
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-11 19:08:27  更:2021-09-11 19:09:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 11:49:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码