基于Unity3D 的Htc vive的基本交互

来源:互联网 发布:入职培训 it 编辑:程序博客网 时间:2024/05/25 23:57

拾取小球 方法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;


        }
    }
}