方案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上可的速度