Tanoda
HideInteractionHandWhenControllerMoving.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity.Query;
10using UnityEngine;
12
14
24 public class HideInteractionHandWhenControllerMoving : MonoBehaviour {
25
28
29 public UnityEvent OnInteractionHandEnabled;
30 public UnityEvent OnInteractionHandDisabled;
31
32 private float _handSeparationDistance = 0.23f;
33 private float _handHoldingDistance = 0.18f;
34
35 private void Reset() {
36 if (intCtrl == null) {
37 intCtrl = GetComponent<InteractionXRController>();
38 }
39 if (intCtrl != null && intHand == null && this.transform.parent != null) {
40 intHand = this.transform.parent.GetChildren().Query()
41 .Select(t => t.GetComponent<InteractionHand>())
42 .Where(h => h != null)
43 .FirstOrDefault(h => h.isLeft == intCtrl.isLeft);
44 }
45 }
46
47 private void Update() {
48 if (intCtrl != null && intCtrl.isActiveAndEnabled && intHand != null) {
49 var shouldIntHandBeEnabled = !intCtrl.isBeingMoved;
50
51 if (intCtrl.isTracked) {
52 var handPos = intHand.position;
53 var ctrlPos = intCtrl.position;
54 var handControllerDistanceSqr = (handPos - ctrlPos).sqrMagnitude;
55
56 // Also allow the hand to be active if it's far enough away from the controller.
57 if (handControllerDistanceSqr > _handSeparationDistance * _handSeparationDistance) {
58 shouldIntHandBeEnabled = true;
59 }
60
61 // Prevent the hand from being active if it's very close to the controller.
62 if (handControllerDistanceSqr < _handHoldingDistance * _handHoldingDistance) {
63 shouldIntHandBeEnabled = false;
64 }
65 }
66
67 if (shouldIntHandBeEnabled && !intHand.enabled) {
68 intHand.enabled = true;
69
71 }
72
73 if (!shouldIntHandBeEnabled && intHand.enabled) {
74 intHand.enabled = false;
75
77 }
78 }
79 }
80
81 }
82
83}
This simple example script disables the InteractionHand script and has event outputs to drive hiding ...
override Vector3 position
Gets the last-tracked position of the underlying Leap hand.
override bool isBeingMoved
Gets whether or not the underlying controller is currently being moved in world space,...
override bool isLeft
Gets whether the controller is a left-hand controller.
override Vector3 position
Gets the last-tracked position of the controller.
override bool isTracked
Gets whether or not the underlying controller is currently tracked and any joystick token filtering h...