Skip to content

Commit 8b8b53f

Browse files
committed
feat(Avatar): add near touch overrides to avatar hand
The Avatar Hand Controller can now accept near touch overrides for each of the finger axes.
1 parent 57ea62d commit 8b8b53f

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

Assets/VRTK/Documentation/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,10 +1176,12 @@ Provides a custom controller hand model with psuedo finger functionality.
11761176
* **Pinky Axis Button:** The button type to listen for axis changes to control the pinky finger.
11771177
* **Three Finger Axis Button:** The button type to listen for axis changes to control the middle, ring and pinky finger.
11781178
* **Thumb State:** The Axis Type to utilise when dealing with the thumb state. Not all controllers support all axis types on all of the available buttons.
1179+
* **Near Touch Overrides:** Finger axis overrides on an Interact NearTouch event.
11791180
* **Touch Overrides:** Finger axis overrides on an Interact Touch event.
11801181
* **Grab Overrides:** Finger axis overrides on an Interact Grab event.
11811182
* **Use Overrides:** Finger axis overrides on an Interact Use event.
11821183
* **Controller Events:** The controller to listen for the events on. If this is left blank as it will be auto populated by finding the Controller Events script on the parent GameObject.
1184+
* **Interact Near Touch:** An optional Interact NearTouch to listen for near touch events on. If this is left blank as it will attempt to be auto populated by finding the Interact NearTouch script on the parent GameObject.
11831185
* **Interact Touch:** An optional Interact Touch to listen for touch events on. If this is left blank as it will attempt to be auto populated by finding the Interact Touch script on the parent GameObject.
11841186
* **Interact Grab:** An optional Interact Grab to listen for grab events on. If this is left blank as it will attempt to be auto populated by finding the Interact Grab script on the parent GameObject.
11851187
* **Interact Use:** An optional Interact Use to listen for use events on. If this is left blank as it will attempt to be auto populated by finding the Interact Use script on the parent GameObject.

Assets/VRTK/Prefabs/AvatarHands/BasicHands/Models/VRTK_BasicHand_AnimationController.controller

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ AnimatorController:
88
m_Name: VRTK_BasicHand_AnimationController
99
serializedVersion: 5
1010
m_AnimatorParameters:
11+
- m_Name: NearTouchState
12+
m_Type: 1
13+
m_DefaultFloat: -1
14+
m_DefaultInt: 0
15+
m_DefaultBool: 0
16+
m_Controller: {fileID: 0}
1117
- m_Name: TouchState
1218
m_Type: 1
1319
m_DefaultFloat: -1

Assets/VRTK/Prefabs/Internal/Scripts/VRTK_AvatarHandController.cs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public class VRTK_AvatarHandController : MonoBehaviour
155155

156156
[Header("Finger Axis Overrides")]
157157

158+
[Tooltip("Finger axis overrides on an Interact NearTouch event.")]
159+
public AxisOverrides nearTouchOverrides;
158160
[Tooltip("Finger axis overrides on an Interact Touch event.")]
159161
public AxisOverrides touchOverrides;
160162
[Tooltip("Finger axis overrides on an Interact Grab event.")]
@@ -166,6 +168,8 @@ public class VRTK_AvatarHandController : MonoBehaviour
166168

167169
[Tooltip("The controller to listen for the events on. If this is left blank as it will be auto populated by finding the Controller Events script on the parent GameObject.")]
168170
public VRTK_ControllerEvents controllerEvents;
171+
[Tooltip("An optional Interact NearTouch to listen for near touch events on. If this is left blank as it will attempt to be auto populated by finding the Interact NearTouch script on the parent GameObject.")]
172+
public VRTK_InteractNearTouch interactNearTouch;
169173
[Tooltip("An optional Interact Touch to listen for touch events on. If this is left blank as it will attempt to be auto populated by finding the Interact Touch script on the parent GameObject.")]
170174
public VRTK_InteractTouch interactTouch;
171175
[Tooltip("An optional Interact Grab to listen for grab events on. If this is left blank as it will attempt to be auto populated by finding the Interact Grab script on the parent GameObject.")]
@@ -220,6 +224,7 @@ protected virtual void OnEnable()
220224
{
221225
animator = GetComponent<Animator>();
222226
controllerEvents = (controllerEvents != null ? controllerEvents : GetComponentInParent<VRTK_ControllerEvents>());
227+
interactNearTouch = (interactNearTouch != null ? interactNearTouch : GetComponentInParent<VRTK_InteractNearTouch>());
223228
interactTouch = (interactTouch != null ? interactTouch : GetComponentInParent<VRTK_InteractTouch>());
224229
interactGrab = (interactGrab != null ? interactGrab : GetComponentInParent<VRTK_InteractGrab>());
225230
interactUse = (interactUse != null ? interactUse : GetComponentInParent<VRTK_InteractUse>());
@@ -308,6 +313,12 @@ protected virtual void SubscribeEvents()
308313
SubscribeButtonAxisEvent(threeFingerAxisButton, ref savedThreeFingerAxisButtonState, threeFingerState, DoThreeFingerAxisEvent);
309314
}
310315

316+
if (interactNearTouch != null)
317+
{
318+
interactNearTouch.ControllerNearTouchInteractableObject += DoControllerNearTouch;
319+
interactNearTouch.ControllerNearUntouchInteractableObject += DoControllerNearUntouch;
320+
}
321+
311322
if (interactTouch != null)
312323
{
313324
interactTouch.ControllerTouchInteractableObject += DoControllerTouch;
@@ -346,6 +357,12 @@ protected virtual void UnsubscribeEvents()
346357
UnsubscribeButtonAxisEvent(savedThreeFingerAxisButtonState, threeFingerState, DoThreeFingerAxisEvent);
347358
}
348359

360+
if (interactNearTouch != null)
361+
{
362+
interactNearTouch.ControllerNearTouchInteractableObject -= DoControllerNearTouch;
363+
interactNearTouch.ControllerNearUntouchInteractableObject -= DoControllerNearUntouch;
364+
}
365+
349366
if (interactTouch != null)
350367
{
351368
interactTouch.ControllerTouchInteractableObject -= DoControllerTouch;
@@ -550,6 +567,32 @@ protected virtual void SetAnimatorStateOff(string state)
550567
animator.SetFloat(state, -1f);
551568
}
552569

570+
protected virtual void DoControllerNearTouch(object sender, ObjectInteractEventArgs e)
571+
{
572+
if (interactTouch != null && interactTouch.GetTouchedObject() == null)
573+
{
574+
SetAnimatorStateOn("NearTouchState", nearTouchOverrides);
575+
HandleOverrideOn(nearTouchOverrides.ignoreAllOverrides, fingerAxis, GetOverridePermissions(nearTouchOverrides), GetOverrideValues(nearTouchOverrides));
576+
}
577+
}
578+
579+
protected virtual void DoControllerNearUntouch(object sender, ObjectInteractEventArgs e)
580+
{
581+
if (interactNearTouch.GetNearTouchedObjects().Count == 0 && (interactTouch == null || interactTouch.GetTouchedObject() == null))
582+
{
583+
for (int i = 0; i < fingerUntouchedAxis.Length; i++)
584+
{
585+
if (!IsButtonPressed(i))
586+
{
587+
SetOverrideValue(i, ref overrideAxisValues, OverrideState.WasOverring);
588+
fingerForceAxis[i] = fingerUntouchedAxis[i];
589+
}
590+
}
591+
SetAnimatorStateOff("NearTouchState");
592+
HandleOverrideOff(nearTouchOverrides.ignoreAllOverrides, GetOverridePermissions(nearTouchOverrides), false);
593+
}
594+
}
595+
553596
protected virtual void DoControllerTouch(object sender, ObjectInteractEventArgs e)
554597
{
555598
SetAnimatorStateOn("TouchState", touchOverrides);
@@ -558,12 +601,15 @@ protected virtual void DoControllerTouch(object sender, ObjectInteractEventArgs
558601

559602
protected virtual void DoControllerUntouch(object sender, ObjectInteractEventArgs e)
560603
{
561-
for (int i = 0; i < fingerUntouchedAxis.Length; i++)
604+
if (interactNearTouch == null || nearTouchOverrides.ignoreAllOverrides)
562605
{
563-
if (!IsButtonPressed(i))
606+
for (int i = 0; i < fingerUntouchedAxis.Length; i++)
564607
{
565-
SetOverrideValue(i, ref overrideAxisValues, OverrideState.WasOverring);
566-
fingerForceAxis[i] = fingerUntouchedAxis[i];
608+
if (!IsButtonPressed(i))
609+
{
610+
SetOverrideValue(i, ref overrideAxisValues, OverrideState.WasOverring);
611+
fingerForceAxis[i] = fingerUntouchedAxis[i];
612+
}
567613
}
568614
}
569615
SetAnimatorStateOff("TouchState");
@@ -702,7 +748,7 @@ protected virtual void ProcessFinger(VRTK_ControllerEvents.AxisType state, int a
702748
}
703749

704750
//Final sanity check, if you're not touching anything but the override is still set, then clear the override.
705-
if ((interactTouch == null || interactTouch.GetTouchedObject() == null) && overrideAxisValues[arrayIndex] != OverrideState.NoOverride)
751+
if (((interactTouch == null && interactNearTouch == null) || (interactNearTouch == null && interactTouch.GetTouchedObject() == null) || (interactNearTouch != null && interactNearTouch.GetNearTouchedObjects().Count == 0)) && overrideAxisValues[arrayIndex] != OverrideState.NoOverride)
706752
{
707753
SetOverrideValue(arrayIndex, ref overrideAxisValues, OverrideState.NoOverride);
708754
}

0 commit comments

Comments
 (0)