text
stringlengths
0
1.11k
Under OVR Skeleton, check Update Root Scale, Enable Physics Capsules, and Apply Bone Translations.
Right hand prefab
Similarly, make sure your settings for the left-hand prefab are the following. (The left-hand option is preselected and for this tutorial you don’t need to enable a physics capsule for the left hand.)
Left hand prefab
On the Hierarchy tab, select first the left-hand OVRHand prefab, and then on the Inspector tab, make sure OVR Skeleton, OVR Mesh, and OVR Mesh Renderer checkboxes are selected. Do the same for the right-hand OVRHand prefab.
Step 4. Set up Cube GameObject
Select the Cube GameObject under the Hierarchy tab.
Change its scale to [0.02, 0.02, 0.02].
Cube scale
Click Add Component, search for Rigidbody and select it. This will enable collision detection against the right hand.
Disable the Use Gravity checkbox in the Rigidbody component.
Click Add Component, search for LineRenderer and select it. This will create the line that will represent the ribbon connecting the cube to the left hand’s index tip.
In the Line Renderer component apply the following settings to update the width of the ribbon (anything below 0.03 m to 0.003 m would do - the smaller the better), avoid casting shadows and generating light data, and add a material that already exists in your project.
Line Renderer
Step 5. Add new script to manage hand tracking
Under Project tab, navigate to the Assets folder.
Right click, select Create > Folder, name it as Scripts, and open this new folder.
Right click, select Create > C# Script, and name it as HandTrackingScript.
Drag the new script onto the Cube GameObject, under the Hierarchy tab.
Select the Cube GameObject, under the Hierarchy tab.
In the Inspector, double click the HandTrackingScript.cs to open it in your IDE of preference.
Step 6. Implement HandTrackingScript.cs
This script manages the hand interaction.
Add variables and objects
In your HandTrackingScript class, add the following:
public Camera sceneCamera;
public OVRHand leftHand;
public OVRHand rightHand;
public OVRSkeleton skeleton;
private Vector3 targetPosition;
private Quaternion targetRotation;
private float step;
private bool isIndexFingerPinching;
private LineRenderer line;
private Transform p0;
private Transform p1;
private Transform p2;
private Transform handIndexTipTransform;
These represent the following:
Variables
Description
sceneCamera
The camera that the scene uses
leftHand and rightHand
The left and right hand prefabs
skeleton
The skeleton (used for retrieving the bones’ position and rotation data)
targetPosition and targetRotation
Position and rotation used for animating the cube while attached to the ribbon)
step
Helps with the animation (Time.deltaTime)
line
The LineRenderer that represents the ribbon
p0, p1, and p2
Starting, bend, and end points’ transforms to help draw the ribbon LineRenderer
handIndexTipTransform
The transform of the left hand index fingertip
Set initial cube’s position in front of user at Start()
In your Start() function, define the initial cube’s position and assign the LineRenderer component to line.
void Start()
{
transform.position = sceneCamera.transform.position + sceneCamera.transform.forward * 1.0f;
line = GetComponent<LineRenderer>();
}
This initially places the cube GameObject in front of the user at a distance of one meter.
Create helper function to place and rotate the cube smoothly
This helper function animates the cube’s reposition and reorientation. Create a new pinchCube() function and add the following lines.
void pinchCube()
{
targetPosition = leftHand.transform.position - leftHand.transform.forward * 0.4f;
targetRotation = Quaternion.LookRotation(transform.position - leftHand.transform.position);
transform.position = Vector3.Lerp(transform.position, targetPosition, step);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, step);
}
This code smoothly places and rotates the cube next to the user’s left hand at a distance of around 0.4 meter. The actual position of the hand is at its wrist. For more information, see Unity’s documentation on Quaternion.LookRotation, Vector3.Lerp, and Quaternion.Slerp.
Create helper function to draw the ribbon