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

236 Views Asked by At

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).

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. 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. Why is this happening?

I've tried adding the line rb.AddRelativeForce(Vector3.forward * magnitude, ForceMode.Force); in OnTouchCompleted which wasn't successful. How can I maintain the ball's motion after it is released by my hand?

1

There are 1 best solutions below

1
derHugo On

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.

Somewhat like e.g. (pseudo code)

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