简体   繁体   English

在 Unity Hololens 2 中释放后如何保持对象的速度?

[英]How can I maintain an object's velocity after release in Unity Hololens 2?

I'm looking not so much for a throw as just maintaining motion after a ball is released by the hand in Hololens 2. Currently, I'm using the MRTK IMixedRealityTouchHandler interface, mainly the functions public void OnTouchStarted(HandTrackingInputEventData data) and OnTouchCompleted(HandTrackingInputEventData data) .在 Hololens 2 中用手释放球后,我正在寻找投掷而不是保持运动。目前,我正在使用 MRTK IMixedRealityTouchHandler接口,主要是函数public void OnTouchStarted(HandTrackingInputEventData data)OnTouchCompleted(HandTrackingInputEventData data)

On the Hololens 2 Emulator, when I release the ball with my hand (mouse), it drifts off in the air in the general direction I was pointing it towards relatively slowly, which is exactly what I want.在 Hololens 2 Emulator 上,当我用手(鼠标)释放球时,它会沿着我指向的大致方向相对缓慢地在空中漂移,这正是我想要的。 I achieved this by reducing drag.我通过减少阻力实现了这一点。 However, once I build to the HL2 device itself, this motion is not emulated and the ball stops midair immediately after it is released.但是,一旦我构建到 HL2 设备本身,就不会模拟这个动作,并且球在释放后立即停在半空中。 Why is this happening?为什么会这样?

I've tried adding the line rb.AddRelativeForce(Vector3.forward * magnitude, ForceMode.Force);我尝试添加行rb.AddRelativeForce(Vector3.forward * magnitude, ForceMode.Force); in OnTouchCompleted which wasn't successful.OnTouchCompleted中没有成功。 How can I maintain the ball's motion after it is released by my hand?手放球后如何保持球的运动?

In general (I don't see the rest of your code) you can keep updating the velocity relative to the last frame and finally apply it.一般来说(我没有看到你的代码的 rest)你可以不断更新相对于最后一帧的速度并最终应用它。

Somewhat like eg (pseudo code)有点像eg(伪代码)

private Vector3 velocity;

void BeginDrag()
{
    rb.isKinematic = true;
    rb.velocity = Vector3.zero;
    lastFramePos = rb.position;
}

void WhileDrag(Vector3 position)
{
    velocity = position -rb.position;
    rb.position = position;
}

void EndDrag()
{
    rb.isKinematic = false;
    rb.velocity = velocity;
}

or actually even easier and probably more accurate you can directly use the或者实际上更容易并且可能更准确你可以直接使用

public void OnTouchCompleted(HandTrackingInputEventData data)
{
    rb.velocity = data.Controller.Velocity;
}

See

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity:如何对物体的速度施加旋转? - Unity: How to apply rotation on object's velocity? 如何在 Unity 中为玩家添加速度? - How can I add velocity to a player in Unity? 如何将 Unity Object 动作与 HoloLens 节目的音乐同步? - How do I synchronize Unity Object actions with music for a HoloLens show? Hololens 2 & Unity - 如何获得 CameraToWorld 和投影矩阵? - Hololens 2 & Unity - How can I get the CameraToWorld and Projection Matrices? 我怎样才能使用Unity2d.velocity跟随Unity中的对象? - How can i use rigidbody2d.velocity to follow an object in Unity? Unity 2D中的速度如何在没有刚体的情况下获得物体的速度 - Velocity in Unity 2D How to get velocity of object without rigidbody 如何将速度应用于忽略 Unity 中子对象质量的父 object? - How to apply velocity to a parent object that ignores child object's mass in Unity? 如何使用 Hololens 2 检测“长按”手势? (带有 MRTK 的 Unity3D) - How can I detect a "long touch" gesture with the Hololens 2? (Unity3D with MRTK) 如何使用 mrtk 2.4 和 Unity 2019.4(用于 Hololens 的 AR)创建具有自定义形状的按钮? - How can I create a button using mrtk 2.4 and Unity 2019.4 (AR for Hololens) with a custom shape? Unity)我怎样才能保持精灵的纵横比? - Unity) How can I maintain sprites aspect ratio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM