Steam VR 2.0 Simple-Complex Grabbing Tutorial Unity Part 2

So this tutorial originally wasn’t meant to be but I figured out that I forgot to include the ability to throw objects in my last one. So forgive me if this one is a little short. The reason that we can’t throw things now is that we are using fixed joints, which means that the velocity of our held object is never set so when we drop it falls like a stone.

To fix this first we need to calculate our hand’s velocity before we throw it, or do we? Most other tutorials I see try to calculate the velocity using the last thirty hand positions or something similar, but what most people don’t realize is that you can grab the velocity from your SteamVR Action Pos just like this:

public SteamVR_Input_Sources Hand;
public SteamVR_Action_Pose position;

private Vector3 velocity;
private void Update()
{
velocity = position.GetVelocity(Hand)
}

And just like that, no math.

To implement this we just need to add the public variables seen above and change the code in our Grab() and Release() code so it looks like this:

     private void Grip()
{
GameObject NewObject = ClosestGrabbable();
if (NewObject != null)
{
ConnectedObject = ClosestGrabbable();//find the Closest Grabbable and set it to the connected object
ConnectedObject.GetComponent().useGravity = false;
}
}
private void Release()
{
GetComponent().connectedBody = null;
GetComponent().connectedBody = null;
ConnectedObject.GetComponent().useGravity = true;
ConnectedObject.GetComponent().velocity = position.GetVelocity(Hand)+transform.parent.GetComponent().velocity;
ConnectedObject.GetComponent().angularVelocity = position.GetAngularVelocity(Hand) + transform.parent.GetComponent().angularVelocity;
ConnectedObject = null;
}

Just a note I added a bit of code that turns gravity on and off to make it act better when held. Also, there is a bit of code that gets the velocity of our Camera rig and adds it to the thrown object, this is because I was using my code for walking (see here) so if you don’t have a rigidbody attached to the parent of your hand you will want to remove that code.

And that’s it for this article, my conscience is clear.

Liked it? Take a second to support WireWhiz on Patreon!
Become a patron at Patreon!