text
stringlengths
0
1.11k
This function draws the LineRenderer ribbon as a Quadratic Bézier curve that consists of 200 segments. A Quadratic Bézier curve draws a path as function B(t), given three points: P0 (start point), P1 (in-between point), and P2 (end point).
The formula is: B(t) = (1 - t)^2 * P0 + 2 * (1 - t) * t * P1 + t^2 * P2.
where:
B, P0, P1, and P2 are Vector3 and represent positions.
t represents the size / portion of the line, so it is between 0 and 1 (included).
For example, if t = 0.5, then B(t) is halfway between point P0 and P2 (passing from P1) and half of the line is drawn.
By using this method, you can start by calculating B(0) and, then, gradually iterate over each segment to draw it.
Add the following to your HandTrackingScript class:
void DrawCurve(Vector3 point_0, Vector3 point_1, Vector3 point_2)
{
line.positionCount = 200;
Vector3 B = new Vector3(0, 0, 0);
float t = 0f;
for (int i = 0; i < line.positionCount; i++)
{
t += 0.005f;
B = (1 - t) * (1 - t) * point_0 + 2 * (1 - t) * t * point_1 + t * t * point_2;
line.SetPosition(i, B);
}
}
The DrawCurve() function requires the start (point_0), the bending point (point_1), and the end point (point_2) Vector3 positions of the line as parameters. It loops until every one of the 200 segments renders.
Confirm left hand is tracked and user is pinching
In your Update() function, add the following:
void Update()
{
step = 5.0f * Time.deltaTime;
if (leftHand.IsTracked)
{
isIndexFingerPinching = leftHand.GetFingerIsPinching(OVRHand.HandFinger.Index);
if (isIndexFingerPinching)
{
line.enabled = true;
// Animate cube smoothly next to left hand
pinchCube();
// ...
}
else
{
line.enabled = false;
}
}
}
This snippet:
Defines your step value to animate the cube. For details, see Unity’s documentation on Time.deltaTime.
Checks if the leftHand OVRHand object is tracked with the isTracked() function. This returns true if that happens.
Calls GetFingerIsPinching(OVRHand.HandFinger.Index) function to confirm that the user is pinching by using their index finger and assigns the returned value to the boolean variable isIndexFingerPinching. This function is defined as bool OVRHand.GetFingerIsPinching (HandFinger finger). For details on the rest of the fingers defined in theHandFinger enum, see OVRHand.
Enables the line LineRenderer if the user is pinching and makes it visible to the user. Otherwise, it disables it.
Calls pinchCube() helper function to place and rotate the cube (if the user is pinching).
Retrieve transform data regarding left hand skeleton’s bones
Amend your Update() function to the following. (Notice the new lines under the pinchCube() invocation.)
void Update()
{
step = 5.0f * Time.deltaTime;
if (leftHand.IsTracked)
{
isIndexFingerPinching = leftHand.GetFingerIsPinching(OVRHand.HandFinger.Index);
if (isIndexFingerPinching)
{
line.enabled = true;
pinchCube();
// New lines added below this point
foreach (var b in skeleton.Bones)
{
if (b.Id == OVRSkeleton.BoneId.Hand_IndexTip)
{
handIndexTipTransform = b.Transform;
break;
}
}
p0 = transform;
p2 = handIndexTipTransform;
p1 = sceneCamera.transform;
p1.position += sceneCamera.transform.forward * 0.8f;
DrawCurve(p0.position, p1.position, p2.position);
// New lines added above this point
}
else
{
line.enabled = false;
}