2017年3月30日 星期四

week06

到s205實作
用手把偵測產生設定好的prefabs物件
然後就可以做出類似畫畫的效果





.

程式碼如下
using UnityEngine;
using System.Collections;
using Valve.VR;

public class Create : MonoBehaviour {

public SteamVR_TrackedObject rightController;
public GameObject redCube;
public GameObject blueCube;
public GameObject box;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)rightController.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
Instantiate (redCube, rightController.transform.position, rightController.transform.rotation, box.transform);

}
if (device.GetPress (SteamVR_Controller.ButtonMask.Trigger)) {
Instantiate (blueCube, rightController.transform.position, rightController.transform.rotation, box.transform);

}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
foreach (Transform child in box.transform) {
GameObject.Destroy (child.gameObject);
}
}
}
}



第二個實作
是要把場景中的物件抓取起來
按下版機就可以當手一樣 把物件抓取起來



程式碼如下:
using UnityEngine;
using System.Collections;

public class Touch : MonoBehaviour {

public SteamVR_TrackedObject leftController;
bool isTouch=false;
// Use this for initialization
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)) {
isTouch = true;
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = false;
}
}
void OnTriggerStay(Collider other){
if (other.tag == "catch" && isTouch) {
other.transform.position = this.transform.position;
}
}
}



第三個實作(回家作業)

將模型套入
讓手把在實境內是以光劍模組顯示








































成功後就可以像絕地武士一樣揮舞光劍了!!!!




程式碼如下:
using UnityEngine;
using System.Collections;

public class LightSaberContol : MonoBehaviour {

public SteamVR_TrackedObject Controller;
public GameObject centerBeam;
lightsaber saberOn,saberCenterBeam;


void Start () {
saberOn = this.GetComponent<lightsaber> ();
saberCenterBeam = centerBeam.GetComponent<lightsaber> ();


}

// Update is called once per frame
void Update () {
if (Controller == null)
return;
var device = SteamVR_Controller.Input ((int)Controller.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Grip)) {
saberOn.saberon = !saberOn.saberon;
saberCenterBeam.saberon = !saberCenterBeam.saberon;
print ("Grip");
}

}
}


沒有留言:

張貼留言