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

左手的控制

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;

    }
}

右手的控制


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;
        }
    }
}



  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值