2017年3月15日 星期三

week04

實際操作VR

辦一個steam帳號


完成後在右下螢幕VR突事按右鍵
執行房間設定


設定房間
水平及高度xyz軸





設定完成後

開啟unity

去asset store

下載steamVR



把main camera刪除

加入camera rig

新增一個plane


執行



畫面中可以看到把手




新增一個
c script


輸入

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

public class abc : MonoBehaviour {

public SteamVR_TrackedObject leftController;

void Start () {

}

// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)leftController.index);
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
print ("Hello World");
}
}



執行後
按下trigger會出現Hello World





HOMEWORK

輸入

using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class myLeftController : MonoBehaviour {
public SteamVR_TrackedObject leftController;
public GameObject controllerInfo;
bool isTriggerPress = false;
bool isTouchPadTouch = false;
bool isTouchPadPress= false;
bool isGripPress= false;
bool isManuPress= false;
Vector2 touchpadPosition=new Vector2();
float pressValue=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
string infor="Left Controller Information : \n";
if (leftController == null)
return;
var device = SteamVR_Controller.Input ((int)leftController.index);
myTriggerDetect (device);
myTouchPadDetect (device);
myGripDetect (device);
myManuDetect (device);
infor += "Press Trigger : " + isTriggerPress + "\nTrigger Press Value :" + pressValue + "\nTouchpad Press :" + isTouchPadPress + "\nTouchpad touch : " + isTouchPadTouch + "\nTouchpad Position :" + touchpadPosition + "\nGrip Press : " + isGripPress + "\nManu Press: " + isManuPress;
controllerInfo.GetComponent<TextMesh> ().text = infor;
}
void myTriggerDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.Trigger)) {
pressValue = device.GetAxis (Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x;
isTriggerPress = true;
} else {
isTriggerPress = false;
pressValue = 0;
}
}
void myTouchPadDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
isTouchPadPress = true;
} else {
isTouchPadPress = false;
}
if (device.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
isTouchPadPress = true;
touchpadPosition = device.GetAxis (Valve.VR.EVRButtonId.k_EButton_Axis0);
} else {
isTouchPadPress = false;
touchpadPosition = new Vector2 (0, 0);
}
}
void myGripDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.Grip)) {
isGripPress = true;
} else {
isGripPress = false;
}
}
void myManuDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.ApplicationMenu)) {
isManuPress = true;
} else {
isManuPress = false;
}
}
}






































加進一個empty
再加一個 text mesh

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

public class myRightcontroller : MonoBehaviour {
public SteamVR_TrackedObject rightController;
public GameObject controllerInfo;
bool isTriggerPress = false;
bool isTouchPadTouch = false;
bool isTouchPadPress= false;
bool isGripPress= false;
bool isMenuPress= false;
Vector2 touchpadPosition=new Vector2();
float pressValue=0;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
string infor="Right Controller Information : \n";
if (rightController == null)
return;
var device = SteamVR_Controller.Input ((int)rightController.index);
myTriggerDetect (device);
myTouchPadDetect (device);
myGripDetect (device);
myManuDetect (device);
infor += "Press Trigger : " + isTriggerPress + "\nTrigger Press Value :" + pressValue + "\nTouchpad Press :" + isTouchPadPress + "\nTouchpad touch : " + isTouchPadTouch + "\nTouchpad Position :" + touchpadPosition + "\nGrip Press : " + isGripPress + "\nManu Press: " + isMenuPress;
controllerInfo.GetComponent<TextMesh> ().text = infor;
}
void myTriggerDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.Trigger)) {
pressValue = device.GetAxis (Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x;
isTriggerPress = true;
} else {
isTriggerPress = false;
pressValue = 0;
}
}
void myTouchPadDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
isTouchPadPress = true;
} else {
isTouchPadPress = false;
}
if (device.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
isTouchPadPress = true;
touchpadPosition = device.GetAxis (Valve.VR.EVRButtonId.k_EButton_Axis0);
} else {
isTouchPadPress = false;
touchpadPosition = new Vector2 (0, 0);
}
}
void myGripDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.Grip)) {
isGripPress = true;
} else {
isGripPress = false;
}
}
void myManuDetect(SteamVR_Controller.Device device)
{
if (device.GetPress (SteamVR_Controller.ButtonMask.ApplicationMenu)) {
isMenuPress = true;
} else {
isMenuPress = false;
}
}
}





































另一隻手

執行






























效果

沒有留言:

張貼留言