steamVR的通过代码实现简单操作

左手的控制

steamVR的通过代码实现简单操作_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class leftss : MonoBehaviour {
    SteamVR_TrackedObject trackedObj;
    SteamVR_Controller.Device device;
    public Transform sphere;//重置功能
    void Awake () {
        trackedObj = GetComponent<SteamVR_TrackedObject> ();
    }
    void FixedUpdate () {
        device = SteamVR_Controller.Input ((int)trackedObj.index);
        if (device.GetTouch (SteamVR_Controller.ButtonMask.Trigger)) {
            Debug.Log ("Touch Trigger");
        }
        if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Trigger)) {
            Debug.Log ("TouchDown Trigger");
        }
        if (device.GetTouchUp (SteamVR_Controller.ButtonMask.Trigger)) {
            Debug.Log ("TouchUp Trigger");
        }
        //把物体拖入实现重置功能
        if (device.GetPressUp (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 rigidbody)
    {
        Transform orgin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
        if (orgin != null) {
            rigidbody.velocity = orgin.TransformVector ((device.velocity));
            rigidbody.angularVelocity = orgin.TransformVector (device.angularVelocity);
        } else 
        {
            rigidbody.velocity = device.velocity;
            rigidbody.angularVelocity = device.angularVelocity;
        }
//        rigidbody.velocity = device.velocity;
//        rigidbody.angularVelocity = device.angularVelocity;

    }
}

右手的控制

steamVR的通过代码实现简单操作_第2张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class rightss : MonoBehaviour {
    //追踪的设备,就是我们的手柄
    SteamVR_TrackedObject trackedObj;
    //获取手柄的输入
    SteamVR_Controller.Device device;
    //固定关节
    FixedJoint fixedJoint;
    //位于手柄上的刚体
    public Rigidbody rigidBodyAttachPoint;
    public Transform sphere;
    void Awake () {
        trackedObj = GetComponent<SteamVR_TrackedObject> ();
    }
    void FixedUpdate () {
        device = SteamVR_Controller.Input ((int)trackedObj.index);
        //重置功能
        if (device.GetPressUp (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 rigidbody)
    {
        Transform orgin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
        if (orgin != null) {
            rigidbody.velocity = orgin.TransformVector ((device.velocity));
            rigidbody.angularVelocity = orgin.TransformVector (device.angularVelocity);
        } else {
            rigidbody.velocity = device.velocity;
            rigidbody.angularVelocity = device.angularVelocity;
        }
    }
}



你可能感兴趣的:(Unity3D)