2017年3月29日 星期三

Week06_Unity VR_isTragger應用_呂登祐



在場景新增一個cube物件,然後再Add Componet增加一個Box Collider做碰撞偵測,在撰寫catchcube.cs碰撞的程式碼(如下),以及Add Componet一個Rigidbody,因為我們要用程式碼去控制手把去拿物體,所以我們要把"Is Kinematic"險項勾選起來,這樣程式碼就可以控制拿物體的時候,手放開Cube不會自己掉下去,而是手放開的時候會停格在半空中。


using UnityEngine;
using System.Collections;
using Valve.VR;

public class catchcube : 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;
}
}


沒有留言:

張貼留言