Unity VR Basics 2023 – 2 Handed Grab

Let’s learn how to make our XR Grab Interactables something that we can grab using both hands! In this tutorial, I’ll also be covering how to change the center of gravity with our game objects so that they might behave as they would in the real world.

Adding A Hammer

For this tutorial, I’ve decided to use a free asset from the Unity asset store.

You can find the sledgehammer I used here. If this asset is taken off the store page ever, you should still be able to follow this tutorial easily with any substitute item.

Once you’ve imported the Game Object into your project, you may need to fix the Material if you’re using the URP.

  •  Assets -> Sledgehammer -> Materials -> SledgeHammer_material
    • In the Inspector change the Shader to Universal Render Pipeline/Lit
    • Select the circle left of Base Map and choose ‘Sledgehammer_Albedo’
    • Select the circle left of Specular Map and choose ‘SledgeHammer_SpecGloss’
This should fix our Material to be able to render properly in the Scene.

Now that we can see our hammer properly, let’s add some Box Colliders to it.

  • Drag Prefab Sledgehammer into the Scene and select it.
    • Add empty Game Object and call it ‘Head’
      • Add Box Collider
        • Center = (0.38, 0, 0)
        • Size = (0.105, 0.19, 0.1)
    • Add empty Game Object and call it ‘Handle’
      • Add Box Collider
        • Center = (0.05, 0,0)
        • Size = (0.76, 0.031, 0.026)
 

Now you should have a hammer that with a two Box Colliders that map out the Head and Handle of the Sledgehammer.

Adjusting Center Of Gravity

The center of gravity for an object tends to vary. This Sledgehammer for example obviously has a heavier head than the handle. Because of that, we should adjust the center of gravity to resemble what it would be like in the real world.

To achieve this, let’s add a script to visualize the center of gravity and adjust it. 

The script above will help us map out the center of gravity of a game object and also draw a Gizmo so we can visualize it. If we return to our scene, we’ll be able to adjust the center of gravity in the Inspector.

Enter play mode and play around the adjusting the center of gravity using the CenterOfGravity Script.

I found that (0.33, 0, 0) was the right feeling center of gravity for myself.

2 Handed Grabbing

In order to grab this with two hands, we’ll need an XR Grab Interactable attached to it.

  • Select the SledgeHammer main object
    • Add ‘XR Grab Interactable’
      • Select Mode = Multiple
Now when we start up the Scene, we should be able to grab the object with both hands!
In order to make it a little more realistic, we should change the following settings.
  •  Select the SledgeHammer main object
    • Rigidbody
      • Collision Detection = Continuous Dynamic
    • XR Grab Interactable
      •  Movement Type = Velocity Tracking
      •  Use Dynamic Attach = True
Now when we start up the Scene, we should be able to grab onto the hammer from any position, while using both hands!
 

Multiple Attach Points

We may want our players to interact with objects in a specific way and not dynamically. This can also help reduce the complexity of interactions our players can have with our objects. To do this, we’re going to need to use Multiple Attach Points.

  • Select the SledgHammer main Objects
    • Create and empty GameObject and name it ‘FirstAttachPoint’
      • Position = (-0.017, 0, 0)
      • Rotation = (-90, 0, -90)
    • Create and empty GameObject and name it ‘SecondAttachPoint’
      • Position = (-0.256, 0, 0)
      • Rotation = (-90, 0, -90)
    • XR Grab Interactable
      • Dynamic Attach = False
      • Attach Transform = FirstAttachPoint
      • Second Attach Transform = SecondAttachPoint

 

Now when we start up the scene, whichever hand grabs first will latch onto the first attach point and the second hand will coordinate with the second attach point!

This is incredibly useful to help players not have to fiddle around with a game object and focus on using it instead!

Conclusion

There is still a ton of things you can add in order to help improve the Two Handed Grab experience. You could try playing with the Smooth Position and Rotation so your object has the right ‘feel’ for your game. Try limiting movement of objects by the X, Y or Z axis so it can be used uniquely in a puzzle.

I hope this introduction to Two Handed Grabbing was useful! See you in the next one!