text
stringlengths
0
1.11k
}
}
The added code does the following:
Iterates through all the bones of the OVRSkeleton object.
This represents the skeleton of the left hand.
Note:skeleton.Bones is an OVRBone list that stores all bones by their Id. For definitions of all the bone Ids, see enum OVRSkeleton.BoneId in OVRSkeleton.
Checks if the OVRSkeleton.BoneId.Hand_IndexTip bone id is found. This represents the left hand’s index tip.
When found, stores that bone’s transform data to variable handIndexTipTransform.
Stores the cube’s transform to p0 and the transform of the left hand’s index tip to p2.
Assigns a transform relating to a position in front of the user’s headpose to p1 . This will serve as the point that bends the LineRenderer.
Calls DrawCurve() helper function to draw the ribbon LineRenderer.
Note: Once comfortable with this tutorial, it is recommended to experiment with the p1 definition and assign different values to it relating to the hand or the cube.
Step 7. Update Cube GameObject and run app
Open Unity Editor, and select the Cube GameObject under the Hierarchy tab.
Under Inspector, select the CenterEyeAnchor camera, which always coincides with the average of the left and right eye poses.
Select the rest of the prefabs as follows.
Script component settings
Save your project, click File > Build And Run.
Put on your headset.
On the headset, ensure that under Settings > Device > Hands & Controllers, the Hand Tracking option is on.
When the app starts, try moving your head, extend, and move your hands to enable hand tracking. When you see the hands rendering, pinch with the left hand and carry the cube through the ribbon. Gently touch the cube with your right hand palm and give it a light push.
Reference script
For future quick reference, here is the complete code in HandTrackingScript.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandTrackingScript : MonoBehaviour
{
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;
// Start is called before the first frame update
void Start()
{
// Set initial cube's position in front of user
transform.position = sceneCamera.transform.position + sceneCamera.transform.forward * 1.0f;
// Assign the LineRenderer component of the cube GameObject to line
line = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
// Define step value for animation
step = 5.0f * Time.deltaTime;
// If left hand is tracked
if (leftHand.IsTracked)
{
// Gather info whether left hand is pinching
isIndexFingerPinching = leftHand.GetFingerIsPinching(OVRHand.HandFinger.Index);
// Proceed only if left hand is pinching
if (isIndexFingerPinching)
{
// Show the Line Renderer
line.enabled = true;
// Animate cube smoothly next to left hand
pinchCube();
// Loop through all the bones in the skeleton
foreach (var b in skeleton.Bones)
{
// If bone is the the hand index tip
if (b.Id == OVRSkeleton.BoneId.Hand_IndexTip)
{
// Store its transform and break the loop
handIndexTipTransform = b.Transform;
break;
}
}