Home and Learn: Games Programming Course


Adding VR Hands

In the previous lesson, we set up our VR rig. You can now move around any VR scene. In this lesson, we'll add a pair of hands so that you can pick things up.

 

Install the Hands

In order to grab things, you're going to need some hands. We're got some for you here, along with some hand animations:

Download VR Hands and Animations (270 Kb)

The hand models in the download are official hands from Meta to used in Oculus VR development. The hand animations, however, are ones we did ourselves.

Download the file above. Unzip it. Now drag and drop the entire folder into the Assets folder in Unity:

Folder being dragged and dropped into the Projects area of Unity

Now expand the Oculus Hands folder in Unity to see what's inside of it:

A folder with 3D chand models and animations in the Projects area of Unity

There should be two hand models, hand_L and hand_R, and a folder called Animations. Double click the Animations folder to see what's inside of it:

A Unity 3D folder showing hand animations

There should be four animations. Later, we'll use these to make a fist when the Grip buttons are pressed on the controllers.

But go back to the Oculus Hands folder. Click on the hand_L model. In the Inspector on the right, switch to the Rig tab. For the Animation Type, make sure it's on Generic. For the Avatar Definition, select Create From This Model from the drop down menu. Then click the Apply button:

A Unity 3D folder showing a hand model selected

Setting up the Rig for the hand model in the Unity Inspector.

When you click Apply, an Avatar will be created under the hand_L model:

An Avatar created from a 3D hand model in Unity 3D.

We'll need this Avatar for the animations later.

Now do the same with the hand_R model: select it in the Oculus Hands folder, switch to the Rig tab, set the Animation Type to Generic and the Avatar Definition to Create From This Model. Then click Apply. You should have Avatars for both of your hand models:

Two Avatars created from a 3D hand models in Unity 3D.

Now click on the hand_L model item again. Drag and drop it onto the Left Controller item in the Hierarchy:

A hand model being dragged onto a game object in the Unity Hiearchy.

What you need is for the hand to be under the Left Controller:

A hand model in the Unity Hiearchy.

With the hand_L item selected in the Hierarchy, have a look at the Inspector on the right. It should look like ours:

A hand model in the Unity Inspector.

Notice that an Animator has been added. The Avatar you created is there, but there is no Controller yet. We'll add one soon.

But do the same for the right-hand model: drag and drop the model called hand_R onto the Right Controller in the Hierarchy.

Now put your VR headset on. You should notice that you have a pair of white hands in front of you:

A 3D game scene showing a pair of white oculus hands.

 

 

Animating VR Hands

Now that we have a pair of hands, let's animate them. What we'll do is to have the hands turn into a pair of fists when the grip button is held on the controllers. This will look better when we're gripping objects on the table.

To make this work, we need to create a Blend Tree, and then add those animations from the folder you saw.

So, in the Projects area at the bottom of Unity, move into the Animations folder. Right click and select Create > Animation Controller. Rename the Controller to AC-Left. Now repeat the process: create a new animation controller. This time, called it AC-Right. You should now have four animations and two controllers:

A Unity folder showing two animation controllers and four animations.

Now to create the Blend Tree.

 

 

Unity Blend Trees

A Blend Tree is away to gather animations under one roof. You can then easily switch between the animations in your code.

To create one, double click your AC-Left animation controller in the Animation folder. You should see this screen appear:

The Unity Animator.

We covered all this in a previous tutorial, so won't go into it again.

To create a Blend Tree, right click in the black area in the middle, but not on one of the colored rectangles. From the menu that appears, select Create State > From New Blend Tree:

Unity context menu showing the From New Blend Tree item selected.

You should see an orange block appear:

Unity animator showing the Blend Tree block selected.

This is the Blend Tree. Notice the line coming from the Entry block to the Blend Tree block. The line is a transition line. We're going from Entry to Blend Tree. (Entry happens when the animation is called.)

Double click the orange Blend Tree block. You should see this:

A Blend Tree in the Unity Animator.

Click on the Blend Tree block to select it. Now look at the Inspector on the right:

Unity Blend Tree in the Inspector

If you wanted to, you could change the name of this Blend Tree by typing a new name at the top, where it says Blend Tree. But leave it on the default. The Blend Type is set to 1D, which is what we want. But click the dropdown to see the other options:

Unity context menu showing the options for a Blend tree motion.

Ours will be a simple animation. But if you had a more complex animation, like run, walk, sprint, etc., then you'd select one of the others. The 2D Cartesion one, for example, can be used to add other animations, ones that you want to morph into other actions. 1D is OK for our purposes, though.

What we want is to add 2 animations, the left idle animation and the left fist animation. Click the plus button under Motion to add one. You'll see a menu:

Adding motion to a Unity Blend Tree.

Select Add Motion Field. Now add a second Motion Field. The Inspector will look like this:

A blend tree in Unity 3D with two motions added.

Notice the two red arrows in the image above. Click the first circle pointed to by the top arrow. Then choose the left_hand_idle animation in the Select Motion box that appears:

Selecting an Animation for the Unity Blend Tree.

Click the second circle pointed to by the red arrow in last but one image above. From the Select Motion box, choose the left_hand_fist animation. Your Inspector should look like this:

Two animations added to the Blend Tree.

We have two Motion animation. When the Grip button is held on the controller, the animation will go from Idle to Fist. When the Grip button is released, the animation will go the other way, from First to Idle. This is our Blend. You can see a visual representation in the middle:

A Belnd Tree in Unity 3D.

Only one thing left to do here, and that's set up a parameter. We can then use this parameter value in our code.

To the left of the Animator, click the Parameter tab:

The Parameter tab in the Unity Animator.

On the Parameter tab, click the down arrow next to the plus button. From the menu, select Float:

Setting a float value as the parameter.

When the Grip is held down, it generates a value between 0 an 1. We'll use this in our script. But type Grip as the name of this Parameter. You can also right click a parameter above, then select Delete from the menu that appears. This will delete a parameter. Delete the Blend parameter to just leave the Grip one:

Setting up a paramater called Grip.

Now that we have set up a parameter called grip, we need to let the Inspector know about it. In the Parameter area, click the dropdown to reveal a menu. All your parameters will be on the menu. We only have one, so select the Grip parameter:

Set the Grip parameter.

The Blend Tree area will look like this:

A Unity Blend Tree.

And that's it. We have finished setting up the Blend Tree for the Animation Controller called AC-Left.

 

Exercise
Set up a Blend Tree for the AC-Right Controller. Add the two animations, right_hand_idle and right_hand_fist. Set a parameter called Grip. (Don't forget to set Grip as a parameter in the Inspector.) When you have finished the exercise above, your Blend Tree area should look like this:

 

Blend tree for right hand animation.

Now that we have set up the two animation controllers, we can set the hands up and write a script to get them working.

 

 

Wire up the Animations

We now need to tell Unity to use those animations we set up. To do that, you only need to add an Animation Controller to the Animator for each hand.

In the Hierarchy on the left, select your hand_L game object under Left Controller. Now look at the Inspector on the right:

The Animator component of a game object in Unity.

We already have the Avatar, which is hand_LAvatar. (We created two hand Avatars earlier, if you remember.) However, the Controller is not using anything (None).

Click the red circle indicated below. From the Select Runtime Animator Controller box that appears, select the AC-Left controller you set up:

Selecting a Controller for the Animator.

While we're here, check the box for Apply Root Motion. And set the Z rotation to 90. This will ensure that our hand model is rotated in the correct direction when we make a fist:

Applying Root Motion to a 3D model in Unity.

Now, under Right Controller in the Inspector, select your hand_R game object. Set the Controller to AC-Right. Make sure the Avatar is hand_RAvatar. Set the Z rotation to -90. Leave the Apply Root Motion unchecked this time:

Settings for the right hand 3D model.

We now need a script to hook everything up. We'll do that in the next lesson below, as this one is getting a bit long. After we add the script, we're done with the hands.

<--Back to the Unity 3D Course Contents Page

 


Email us: enquiry at homeandlearn.co.uk