基於Unity3D 的Htc vive的基本互動
拾取小球 方法1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))] ///這個元件是控制和跟蹤遊戲物件;
public class PickUp : MonoBehaviour {
SteamVR_TrackedObject tracjedObj; // 新增一個控制與追蹤的元件
SteamVR_Controller.Device device;
public Transform sphere;
void Awake () {
tracjedObj = GetComponent<SteamVR_TrackedObject>();
}
void FixedUpdate () {
device = SteamVR_Controller.Input((int)tracjedObj.index); // 先拿到裝置的一個輸入的處理
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) // 檢測到按著Trigger鍵的時候觸發
{
Debug.Log("Touch Trigger");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetTouchUp Trigger");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetTouchUp Trigger");
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
sphere.transform.position = Vector3.zero;
sphere.GetComponent<Rigidbody>().velocity = Vector3.zero;
sphere.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
void OnTriggerStay(Collider collider)
{
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
collider.attachedRigidbody.isKinematic = true;
collider.gameObject.transform.SetParent(gameObject.transform);
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
collider.attachedRigidbody.isKinematic = false ;
collider.gameObject.transform.SetParent(null);
tossObject(collider.attachedRigidbody/*碰撞器與對撞機相連。*/);
}
}
void tossObject(Rigidbody rigidaby)
{
Transform origin = tracjedObj.origin ? tracjedObj.origin : tracjedObj.transform.parent;
if (origin!=null)
{
rigidaby.velocity = origin.TransformVector(device.velocity);
rigidaby.angularVelocity = origin.TransformVector(device.angularVelocity);
}
else
{
rigidaby.velocity = device.velocity;
rigidaby.angularVelocity = device.angularVelocity;
}
}
}
方法2: 使用固定關節
現在右手柄新增碰撞器勾選IsTrigger 然後新增Rigibody 取消勾選重力和勾選IsKinematic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))] ///這個元件是控制和跟蹤遊戲物件;
public class VRFixedJoint : MonoBehaviour {
SteamVR_TrackedObject tracjedObj; // 新增一個控制與追蹤的元件
SteamVR_Controller.Device device; //獲取手柄的輸入
// 固定關節
FixedJoint fixedJoint;
//位於手柄上的剛體
public Rigidbody rigidBodyAttachPoint;
public Transform sphere;
// Use this for initialization
void Awake()
{
tracjedObj = GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void FixedUpdate() {
device = SteamVR_Controller.Input((int)tracjedObj.index); // 先拿到裝置的一個輸入的處理
//重置功能
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
sphere.transform.position = Vector3.zero; //重置位置
sphere.GetComponent<Rigidbody>().velocity = Vector3.zero;//重置速度
sphere.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;//重置角度
}
}
void OnTriggerStay(Collider collider)
{
if (fixedJoint==null && device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
//吧關節元件新增到例項化物件上 連線的位置就是這個剛體
fixedJoint = collider.gameObject.AddComponent<FixedJoint>();
fixedJoint.connectedBody = rigidBodyAttachPoint;
}
else if((fixedJoint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)))
{
GameObject go = fixedJoint.gameObject;//獲取關節上的遊戲物件
Rigidbody rigidbody = go.GetComponent<Rigidbody>();//獲取剛體
//銷燬
Object.Destroy(fixedJoint);
fixedJoint = null;
tossObject(rigidbody);
}
}
void tossObject(Rigidbody rigidaby)
{
Transform origin = tracjedObj.origin ? tracjedObj.origin : tracjedObj.transform.parent;
if (origin != null)
{
rigidaby.velocity = origin.TransformVector(device.velocity);
rigidaby.angularVelocity = origin.TransformVector(device.angularVelocity);
}
else
{
rigidaby.velocity = device.velocity;
rigidaby.angularVelocity = device.angularVelocity;
}
}
}