方案1:VRTK原生api方式
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()));
}
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;
}
但是這樣得到速度很不準确,有時候靜止也會有速度産生。并且初始化條件苛刻,要開始運作時,搖桿就處于激活狀态。
protected virtual void Update()
{
float dis = Vector3.Distance(transform.position, m_lastPos);
float speed = dis / Time.deltaTime;
m_lastPos = transform.position;
}