Unity與VR設(shè)備的交互

usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

///

///檢測手柄功能的腳本,這個腳本掛載在手柄上(Controller(Left)/Controller(Right))

///

public class TrackedEvent:MonoBehaviour{

//手柄

SteamVR_TrackedObject trackedObject;

voidStart(){

//獲得手柄上的腳本組件

trackedObject=this.GetComponent<SteamVR_TrackedObject>();

}

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();//添加固定關(guān)節(jié)

m_joint.connectedBody=gameObject.GetComponent();

}

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

//獲取關(guān)節(jié)上的物體

GameObjectm_object=m_joint.gameObject;

Rigidbodym_rigidbody=m_joint.GetComponent<Rigidbody>();

DestroyImmediate(m_joint);

//模擬實現(xiàn)將物體真實拋出

//初速度

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;

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容