Unity 中 XR 控制器移动的距离
Distance moved by XR-controller in Unity
我一直在 Unity 中制作一个脚本来测量玩家在现实世界中使用像这样的 XRNodes 用右手移动了多远:
InputTracking.GetLocalPosition(XRNode.RightHand)
在运动的开始,然后将其与结束位置进行比较
现在我想移动距离,即使玩家绕着一圈移动。
是使用 XRNodes 执行此操作的方法吗?测量游戏过程中移动的总距离?
是的,好吧,你可以简单地总结每一帧,比如
// Stores the overall moved distance
private float totalMovedDistance;
// flag to start and stop tracking
// Could also use a Coroutine if that fits you better
private bool track;
// Store position of last frame
private Vector3 lastPos;
public void BeginTrack()
{
// reset total value
totalMovedDistance = 0;
// store first position
lastPos = InputTracking.GetLocalPosition(XRNode.RightHand);
// start tracking
track = true;
}
public void EndTrack()
{
// stop tracking
track = false;
// whatever you want to do with the total distance now
Debug.Log($"Total moved distance in local space: {totalMovedDistance}", this);
}
private void Update()
{
// If not tracking do nothing
if(!track) return;
// get current controller position
var currentPos = InputTracking.GetLocalPosition(XRNode.RightHand);
// Get distance moved since last frame
var thisFrameDistance = Vector3.Distance(currentPos, lastPos);
// sum it up to the total value
totalMovedDistance += thisFrameDistance;
// update the last position
lastPos = currentPos;
}
我一直在 Unity 中制作一个脚本来测量玩家在现实世界中使用像这样的 XRNodes 用右手移动了多远:
InputTracking.GetLocalPosition(XRNode.RightHand)
在运动的开始,然后将其与结束位置进行比较
现在我想移动距离,即使玩家绕着一圈移动。
是使用 XRNodes 执行此操作的方法吗?测量游戏过程中移动的总距离?
是的,好吧,你可以简单地总结每一帧,比如
// Stores the overall moved distance
private float totalMovedDistance;
// flag to start and stop tracking
// Could also use a Coroutine if that fits you better
private bool track;
// Store position of last frame
private Vector3 lastPos;
public void BeginTrack()
{
// reset total value
totalMovedDistance = 0;
// store first position
lastPos = InputTracking.GetLocalPosition(XRNode.RightHand);
// start tracking
track = true;
}
public void EndTrack()
{
// stop tracking
track = false;
// whatever you want to do with the total distance now
Debug.Log($"Total moved distance in local space: {totalMovedDistance}", this);
}
private void Update()
{
// If not tracking do nothing
if(!track) return;
// get current controller position
var currentPos = InputTracking.GetLocalPosition(XRNode.RightHand);
// Get distance moved since last frame
var thisFrameDistance = Vector3.Distance(currentPos, lastPos);
// sum it up to the total value
totalMovedDistance += thisFrameDistance;
// update the last position
lastPos = currentPos;
}