基于Unity3D 的Htc vive的基本交互
来源:互联网 发布:入职培训 it 编辑:程序博客网 时间:2024/05/25 23:57
拾取小球 方法1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))] ///这个组件是控制和跟踪游戏对象;
public class PickUp : MonoBehaviour {
SteamVR_TrackedObject tracjedObj; // 添加一个控制与追踪的组件
SteamVR_Controller.Device device;
public Transform sphere;
void Awake () {
tracjedObj = GetComponent<SteamVR_TrackedObject>();
}
void FixedUpdate () {
device = SteamVR_Controller.Input((int)tracjedObj.index); // 先拿到设备的一个输入的处理
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) // 检测到按着Trigger键的时候触发
{
Debug.Log("Touch Trigger");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetTouchUp Trigger");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetTouchUp Trigger");
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
sphere.transform.position = Vector3.zero;
sphere.GetComponent<Rigidbody>().velocity = Vector3.zero;
sphere.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
void OnTriggerStay(Collider collider)
{
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
collider.attachedRigidbody.isKinematic = true;
collider.gameObject.transform.SetParent(gameObject.transform);
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
collider.attachedRigidbody.isKinematic = false ;
collider.gameObject.transform.SetParent(null);
tossObject(collider.attachedRigidbody/*碰撞器与对撞机相连。*/);
}
}
void tossObject(Rigidbody rigidaby)
{
Transform origin = tracjedObj.origin ? tracjedObj.origin : tracjedObj.transform.parent;
if (origin!=null)
{
rigidaby.velocity = origin.TransformVector(device.velocity);
rigidaby.angularVelocity = origin.TransformVector(device.angularVelocity);
}
else
{
rigidaby.velocity = device.velocity;
rigidaby.angularVelocity = device.angularVelocity;
}
}
}
方法2: 使用固定关节
现在右手柄添加碰撞器勾选IsTrigger 然后添加Rigibody 取消勾选重力和勾选IsKinematic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))] ///这个组件是控制和跟踪游戏对象;
public class VRFixedJoint : MonoBehaviour {
SteamVR_TrackedObject tracjedObj; // 添加一个控制与追踪的组件
SteamVR_Controller.Device device; //获取手柄的输入
// 固定关节
FixedJoint fixedJoint;
//位于手柄上的刚体
public Rigidbody rigidBodyAttachPoint;
public Transform sphere;
// Use this for initialization
void Awake()
{
tracjedObj = GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void FixedUpdate() {
device = SteamVR_Controller.Input((int)tracjedObj.index); // 先拿到设备的一个输入的处理
//重置功能
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
sphere.transform.position = Vector3.zero; //重置位置
sphere.GetComponent<Rigidbody>().velocity = Vector3.zero;//重置速度
sphere.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;//重置角度
}
}
void OnTriggerStay(Collider collider)
{
if (fixedJoint==null && device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
//吧关节组件添加到实例化对象上 连接的位置就是这个刚体
fixedJoint = collider.gameObject.AddComponent<FixedJoint>();
fixedJoint.connectedBody = rigidBodyAttachPoint;
}
else if((fixedJoint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)))
{
GameObject go = fixedJoint.gameObject;//获取关节上的游戏对象
Rigidbody rigidbody = go.GetComponent<Rigidbody>();//获取刚体
//销毁
Object.Destroy(fixedJoint);
fixedJoint = null;
tossObject(rigidbody);
}
}
void tossObject(Rigidbody rigidaby)
{
Transform origin = tracjedObj.origin ? tracjedObj.origin : tracjedObj.transform.parent;
if (origin != null)
{
rigidaby.velocity = origin.TransformVector(device.velocity);
rigidaby.angularVelocity = origin.TransformVector(device.angularVelocity);
}
else
{
rigidaby.velocity = device.velocity;
rigidaby.angularVelocity = device.angularVelocity;
}
}
}
- 基于Unity3D 的Htc vive的基本交互
- HTC vive基于unity的凝视交互功能(带HTC 插件)
- 基于 OSG OpenSceneGraph HTC Vive 的实现
- HTC VIVE开发笔记(一)使用SteamVR插件进行基本的交互
- HTC VIVE的交互(凝视,瞬移,发出射线)
- HTC VIVE出现的问题
- Htc Vive的安装调试
- 三分钟用Unity3D开发第一个HTC VIVE的VR程序
- Unity3d开发 HTC vive手柄控制 手柄上各个按钮的点击事件
- HTC Vive的Lighthouse工作原理
- htc vive漂亮的激光线效果
- HTC Vive 扔球换材质的效果
- Unity HTC vive 物体的拾取
- 基于htc vive设备的UE4 VR项目开发需要修改源码的地方
- HTC Vive初体验 简单场景的搭建 基本的输入检测
- 基于HTC vive 的 VR场景遍历及环境互动项目规划
- HTC VIVE开发笔记(六)VRTK中实现基本抓取物体的配置
- HTC Vive之Unity3d开发日记——手柄交互编程
- 实验吧keyboard
- python求解重叠区域线段覆盖总长度
- Ubuntu 16.0.4 Linux下安装Docker
- bootstrap轮播图与现实问题和active使用等
- POJ-2407-Relatives(欧拉函数模板题)
- 基于Unity3D 的Htc vive的基本交互
- 23种设计模式汇总整理
- 23. Merge k Sorted Lists
- Linux系统管理与设置
- sql语句行列转至
- 如何拥有赏(sao)心(de)悦(yi)目(bi)的开发环境
- 基本数据类型包装数据类型之间的转换
- 嵌入式软件的移植的一般步骤
- 选择排序法(Selection sorting)