unity3d:htcvive vrtk获取手柄的速度

2023-08-24 14:52:33 浏览数 (1)

方案1:VRTK原生api方式

代码语言:javascript复制
    void CtrlReferInit1()
    {
        m_leftCtrlRefer = VRTK_ControllerReference.GetControllerReference(m_leftController);

        Debug.Log("Left: "   VRTK_SDK_Bridge.GetControllerIndex(VRTK_DeviceFinder.GetControllerRightHand()));


        m_rightCtrlRefer = VRTK_ControllerReference.GetControllerReference(m_rightController);

        Debug.Log("Right:"   VRTK_SDK_Bridge.GetControllerIndex(VRTK_DeviceFinder.GetControllerRightHand()));
    }
代码语言:javascript复制
public float GetControllerVelocity(SDK_BaseController.ControllerHand handType )
    {
        float speed = 0;
        switch (handType)
        {
            case SDK_BaseController.ControllerHand.Left:
                speed = VRTK_DeviceFinder.GetControllerVelocity(m_leftCtrlRefer).magnitude;
                break;
            case SDK_BaseController.ControllerHand.Right:
                speed = VRTK_DeviceFinder.GetControllerVelocity(m_rightCtrlRefer).magnitude;
                break;
            default:
                break;
        }
        return speed;
    }

但是这样得到速度很不准确,有时候静止也会有速度产生。并且初始化条件苛刻,要开始运行时,手柄就处于激活状态。

方案2:v= s/t

代码语言:javascript复制
        protected virtual void Update()
        {
            float dis = Vector3.Distance(transform.position, m_lastPos);
            float speed = dis / Time.deltaTime;
            m_lastPos = transform.position;
         }

把这方法挂在手柄GameObject上可的速度

0 人点赞