VR Development — Day #3
The ability to grab objects in the VR world
In this article, we will look at two ways for grabbing an object. First is using Kinematics technique, then have a look at using Fixed Joint technique, and then we will settle on combining the two.
Kinematic Grabbing
This technique is making the grabbed object a child of the hand, making it kinematic during that phase ensures that gravity does not have an influence on it, and if it bumps into something, it won’t fly away.
In the GrabbableObject script, make the following changes.
In the Grabber script, make the following changes. This will check if the grip button has been pressed and call the object's grabbable code.
Setup scene
I am going to into Synty Studios POLYGON Starter Pack — Low Poly 3D Art by Synty. It is free in the Unity Asset Store. Import into the project.
Go to Edit > Render Pipeline > Universal Render Pipeline > Upgrade Project Materials to UniversalRP Materials and click Proceed to fix the materials.
I did a little bit of level design by creating a level and throwing in some props. I put a Rigidbody and the GrabbableObject script on the sword and two water pistols. This is how it runs so far.
As a small improvement, we know that when we put a GrabbableObject script on an object, we need to add a Rigidbody component too. To help automatically do this, we use the RequiredComponent attribute. Add this to the top of the class of the GrabbableObject script.
Now whenever you drag on the GrabbableObject script onto an object, it will automatically add a Rigidbody component if there is not already one attached to the game object.
Now you may notice, that when I grab the gun, it can go through the block. It is a drawback of the Kinematic approach. This now leads us to the next technique, Fixed Joints.
To address this, we will add a fixed joint to the game object and when you push the gun into a solid object when it reaches a certain “break force”, it gun should break from your hands.
Fixed Joint Grabbing
Let’s make the changes to the GrabbableObject script to use the Fixed Joint component.
Press play and see the result.
You may have noticed there is a slight jitter occurring. This is caused by the transform position occurring with the Physics time stepper and not matching the game frame rate. How do we combat this? We address this problem by combining the two techniques.
Make the following changes in the GrabbableObject script
Now the grabbed object moves smoothly with the hand while maintaining the Fixed Joint grabbing technique.
Our demo is coming along nicely. We can now grab objects in the world and have them interact with other objects using physics correctly. In the next article, we will explore how to throw objects.
If you found this article helpful, please give me a Clap, as this brightens my day knowing I may have helped someone in their journey in making games. Also if you would like to see more, “Follow” me, so you will be notified of future releases. You may also send me a message if you need any further help.