Unity与VR设备的交互

usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

///

///检测手柄功能的脚本,这个脚本挂载在手柄上(Controller(Left)/Controller(Right))

///

public class TrackedEvent:MonoBehaviour{

//手柄

SteamVR_TrackedObject trackedObject;

voidStart(){

//获得手柄上的脚本组件

trackedObject=this.GetComponent();

}

voidUpdate(){//获取手柄输入

var device=SteamVR_Controller.Input((int)trackedObject.index);

//每一种按钮都有六个方法GetTouch,GetTouchDown,GetTouchUp,GetPress,GetPressDown,GetPressUp

if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)){

//手柄震动反馈

device.TriggerHapticPulse();

//获取右手手柄

vardeviceIndex2=SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);

vardeviceRight=SteamVR_Controller.Input(deviceIndex2);

Debug.Log("轻触了扳机");

}

if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)){

}

if(device.GetPress(SteamVR_Controller.ButtonMask.Touchpad)){

}

//圆盘交互

if(device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)){

Debug.Log("按下圆盘");

//获取按下圆盘的位置

Vector2p=device.GetAxis();

floatangle=VectorAngle(newVector2(1,0),p);

if(angle>45&&angle<135){

Debug.Log("上");

}

if(angle<-45&&angle>-135){

Debug.Log("下");

}

if(angle<180&&angle>135||angle<-135&&angle>-180){

Debug.Log("左");

}

if(angle<45&&angle>-45){

Debug.Log("右");

}

}

}

floatVectorAngle(Vector2first,Vector2second){

floatangle;

Vector3cross=Vector3.Cross(first,second);

angle=Vector2.Angle(first,second);

returncross.z>0?(-angle):angle;

}




usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

publicclassCatchGameObject:MonoBehaviour{

//抓取到的物体

GameObjecttarget;

//得到当前手柄

SteamVR_TrackedObjecttrackedObject;

FixedJointm_joint;

voidStart(){

trackedObject=transform.parent.gameObject.GetComponent();

}

voidUpdate(){

vardevice=SteamVR_Controller.Input((int)trackedObject.index);

if(target!=null&&device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)){

target.transform.position=transform.position;

m_joint=target.AddComponent();//添加固定关节

m_joint.connectedBody=gameObject.GetComponent();

}

if(m_joint!=null&&device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)){

//获取关节上的物体

GameObjectm_object=m_joint.gameObject;

Rigidbodym_rigidbody=m_joint.GetComponent();

DestroyImmediate(m_joint);

//模拟实现将物体真实抛出

//初速度

m_rigidbody.velocity=device.velocity;

m_rigidbody.angularVelocity=device.angularVelocity;

m_rigidbody.maxAngularVelocity=m_rigidbody.angularVelocity.magnitude;

}

}

voidOnTriggerStay(Collider  other){

target=other.gameObject;

}

voidOnTriggerExit(Colliderother){

target=null;

}

}

}

你可能感兴趣的:(Unity与VR设备的交互)