Unity VR : Making Grabbable Objects Even Better

Unity VR: Making Grabbable Objects Even Better

In the previous tutorial, which can be found here, we made interactable objects. The problem is, if we added a baseball and bowling ball, they’d all act the same! The ball also just clips through our hand if we don’t hide the hand prefab.

In this tutorial, we’re going to cover how to make our grabbable objects even better by playing with the Collision Detection, Movement Type, Smooth Position and Smooth Rotation, and adding Physics Materials.  We’ll also cover how to map certain objects to our hands by providing anchor points for them to attach.

I also provide a free bat prefab about halfway through this tutorial. It kind of looks like a balloon bat, but whatevers.

Step 1 : Determine What Kind of Collision Detection We Should Use

If you look at our three balls we created from the last tutorial, you’ll notice they all have Rigidbodies. These Rigidbodies have an attribute called Collision Detection, which gives us for options to choose from. As the name suggests, it will determine how Unity will calculate collision between this object and all other objects with meshes.

Discrete –  This mode will drain the least amount of resources. It typically works well enough, but if an object is moving too fast, it might fall through another mesh since this doesn’t update as frequently as the other options.

Continuous –  This option will drain more resources to detect collisions more frequently if the object is moving fast. This is used for when objects are mostly interacting with game objects without a rigidbody (static). 

Continuous Dynamic –  This also drains a lot of resources to produce better collision detection. The difference is that this type will work for both objects without rigidbodies (static) and with rigidbodies (dynamic). 

Continuous Speculative- This mode works like Continuous Dynamic, but tries to save some resources while doing so. The reduction in resources does cause for less accuracy however.

Conclusion… Well that depends on your use case. It’s important to save as many resources as possible when developing VR games. Making everything Continuous Dynamic can overload an Oculus Quest pretty quickly so it should be used sparingly. 

For our use case however, we only have three balls and not 100.

Select every ball and switch the Collision Detection to Continuous Dynamic.

Step 2 : Determine What Kind of Movement Type We Should Use

The XR Grab Interactable script has a attribute called movement type that I already covered the basics of here

Again, we have to ask what are our use cases?

Are we trying to make a cabinet door that opens? Well we should try using Velocity Tracking

Do we want to have the object act like it would in the real world while we’re holding it but don’t want it running into all colliders when holding it? Kinematic is our most likely candidate.

Do we not care about realism and just want it to track smoothly and effortlessly with our hands? Instantaneous  is the answer mi amigo

For this tutorial, I want to change the three balls to Kinematic. This will allow us to play with a few more controls in the next step. (I’ll cover Velocity Tracking when I do a tutorial on making doors/drawers).

Step 3 : Smooth Position and Smooth Rotation

If you recall, setting the Movement Type to Kinematic will cause our Object’s movement to look jagged when we have it in our hand.

Kinematic

We can actually smooth out this jaggedness by playing with the Smooth Position and Smooth Rotation attributes. Once checked, each attribute has Smooth Position(Rotation) amount and Tighten Position(Rotation). 

Smooth Position (Rotation) Amount –  This determines how closely the object will follow the Interactor. The higher the amount, the closer it will follow.

Tighten Position(Rotation) –  This determines how much smoothing will take place. The lower the number, the more smoothing. A setting of 1 means no smoothing at all.

What all this means is difficult to demonstrate with words or even animated gifs. I’ve provided one to help, but it still won’t get the point across until you try it yourself. In this gif, I’ve lowered the Smooth Position Amount to 0 and also lowered the Tighten Position down to 0. The result? Well you look like a magician since one attribute is telling the object to not follow the Interactor closely and the other is saying use as much smoothing as possible.

 I know it’s hard work, but the only way to make objects ‘feel’ right is by tweeking these values and trying it in game.

Step 4 : Add Physics Material

A baseball and a bowling ball don’t typically act the same when you drop them. Not only do they have different mass, but they are made of a different material too!

To fix our balls from being so boring, lets add some physics materials. You can do this by right clicking in the project, go to create and choosing Physic Material.

Name it Bouncy and give it a Bounciness of .5 in the Inspector. Make another Physics Material and call it No Bounce, setting its Bounciness to 0. 

Now select one of the balls in the scene. Under Sphere Collider, attach one of the new Physics Materials to the Material attribute.

Attach the other Physics Material to a different ball within the scene and test things out. This is another one that you’ll need to play with the bounciness level in order to make sure your objects feel right in VR.

Step 5.... FREE BAT PREFAB!

Since you’ve made it this far, enjoy a free bat prefab that I made. You can download it here. Once downloaded, add the bat.fbx file to your project. Make a new Material and call it BatBase and select its color to be Brown. Make a Second Material and call it BatHandle, making its color black. Now select the bat.fbx file within the project and navigate to Materials in the Inspector window. Apply BatBase to the first Material slot and BatHandle to the second Material slot. Boom. Free bat.

Step 6 : Adding Attach Points To Grabbable Objects

Start by adding the bat to the scene. We need to add a few components to it and adjust things a bit.

  • Add a Box Collider
  • Add a XR Grab Interactable
  • In Rigidbody, Set Collision Detection to Continuous Dynamic
  • In XR Grab Interactable, Set Movement Type to Kinematic

Now, if you were to press play and grab the bat, you’ll notice that it will attach our hand to the middle of the bat. Since we probably want to use this bat like a normal person, we need to set an Attach Point.

  • Create an empty object on the bat inside the scene. Name it Attach Point. 
  • Give it’s transform the following attributes: Position (.00025, 0, .0045), Rotation (-90, 0, 0), Scale (1, 1, 1). 

What we’ve just done is set where our hand is going to attach and what direction the object will face. 

To understand how to set up Anchor Points correctly, we need to look at the X(red), Y(green) and Z(blue) Axis. 

  • The Y Axis should be pointing up in the direction we want our object to point up. 
  • The X Axis should be pointing towards where we want the palm of our hand. 
  • The Z Axis we want to have facing in the direction we want our object to face.
Take note that this should all be based off of local rotation and not global.

Last, we need to let the XR Grab Interactable script know that this is our attach point by assigning it.

Now when you start up the scene you should be able to attach the bat to your hand as excpected!

That's it!

These are just a few of the tools you can use to make your grabbable objects even better. There is a lot of fine tuning that can go into all of these things and it’s important to do so. The more immersive experience we can give the player the better. When working in VR, breaking immersion should be avoided as much as possible and these little tools will help you on your way to accomplishing that.