Skip to content

Commit b744ade

Browse files
committed
fix(Pointer): defer controller setup to Update method
Previously, the Pointer script would attempt to set up the controller for the pointer in the OnEnable and then the Start method, however this caused issues when wanting to use the `activate on enable` setting along with the `interact with objects` setting as the pointer was enabled before the controller interactions were created meaning it would not work. This fix now ensures the pointer controller set up is done at the first possible moment in the update routine after all enable and start methods have been run. This will also mean that the disabling and enabling of a pointer will still work and set up correctly.
1 parent 383ec8f commit b744ade

File tree

1 file changed

+61
-35
lines changed

1 file changed

+61
-35
lines changed

Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class VRTK_Pointer : VRTK_DestinationMarker
9595
protected bool canClickOnHover;
9696
protected bool activationButtonPressed;
9797
protected bool selectionButtonPressed;
98+
protected bool attemptControllerSetup;
9899

99100
public virtual void OnActivationButtonPressed(ControllerInteractionEventArgs e)
100101
{
@@ -152,7 +153,7 @@ public virtual bool IsSelectionButtonPressed()
152153
/// <param name="givenHit">The valid collision.</param>
153154
public virtual void PointerEnter(RaycastHit givenHit)
154155
{
155-
if (enabled && givenHit.transform && controllerIndex < uint.MaxValue)
156+
if (enabled && givenHit.transform != null && controllerIndex < uint.MaxValue)
156157
{
157158
SetHoverSelectionTimer(givenHit.collider);
158159
OnDestinationMarkerEnter(SetDestinationMarkerEvent(givenHit.distance, givenHit.transform, givenHit, givenHit.point, controllerIndex, false, GetCursorRotation()));
@@ -167,7 +168,7 @@ public virtual void PointerEnter(RaycastHit givenHit)
167168
public virtual void PointerExit(RaycastHit givenHit)
168169
{
169170
ResetHoverSelectionTimer(givenHit.collider);
170-
if (givenHit.transform && controllerIndex < uint.MaxValue)
171+
if (givenHit.transform != null && controllerIndex < uint.MaxValue)
171172
{
172173
OnDestinationMarkerExit(SetDestinationMarkerEvent(givenHit.distance, givenHit.transform, givenHit, givenHit.point, controllerIndex, false, GetCursorRotation()));
173174
StopUseAction();
@@ -243,24 +244,12 @@ protected override void OnEnable()
243244
{
244245
base.OnEnable();
245246
VRTK_PlayerObject.SetPlayerObject(gameObject, VRTK_PlayerObject.ObjectTypes.Pointer);
246-
customOrigin = (customOrigin == null ? VRTK_SDK_Bridge.GenerateControllerPointerOrigin(gameObject) : customOrigin);
247-
SetupController();
248-
SetupRenderer();
249-
activateDelayTimer = 0f;
250-
selectDelayTimer = 0f;
251-
hoverDurationTimer = 0f;
252-
currentActivationState = 0;
253-
wasActivated = false;
254-
willDeactivate = false;
255-
canClickOnHover = false;
247+
SetDefaultValues();
248+
256249
if (NoPointerRenderer())
257250
{
258251
VRTK_Logger.Warn(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_PARAMETER, "VRTK_Pointer", "VRTK_BasePointerRenderer", "Pointer Renderer"));
259252
}
260-
if (activateOnEnable)
261-
{
262-
Toggle(true);
263-
}
264253
}
265254

266255
protected override void OnDisable()
@@ -270,14 +259,47 @@ protected override void OnDisable()
270259
UnsubscribeSelectionButton();
271260
}
272261

273-
protected virtual void Start()
262+
protected virtual void Update()
274263
{
275-
FindController();
264+
AttemptControllerSetup();
265+
CheckButtonSubscriptions();
266+
HandleEnabledPointer();
267+
UpdateDirectionIndicator();
276268
}
277269

278-
protected virtual void Update()
270+
protected virtual void SetDefaultValues()
271+
{
272+
customOrigin = (customOrigin == null ? VRTK_SDK_Bridge.GenerateControllerPointerOrigin(gameObject) : customOrigin);
273+
SetupRenderer();
274+
activateDelayTimer = 0f;
275+
selectDelayTimer = 0f;
276+
hoverDurationTimer = 0f;
277+
currentActivationState = 0;
278+
wasActivated = false;
279+
willDeactivate = false;
280+
canClickOnHover = false;
281+
attemptControllerSetup = true;
282+
}
283+
284+
protected virtual void AttemptControllerSetup()
285+
{
286+
if (attemptControllerSetup)
287+
{
288+
if (FindController())
289+
{
290+
attemptControllerSetup = false;
291+
SetupController();
292+
SetupRenderer();
293+
if (activateOnEnable)
294+
{
295+
Toggle(true);
296+
}
297+
}
298+
}
299+
}
300+
301+
protected virtual void HandleEnabledPointer()
279302
{
280-
CheckButtonSubscriptions();
281303
if (EnabledPointerRenderer())
282304
{
283305
pointerRenderer.InitalizePointer(this, invalidListPolicy, navMeshCheckDistance, headsetPositionCompensation);
@@ -287,11 +309,8 @@ protected virtual void Update()
287309
bool currentPointerVisibility = pointerRenderer.IsVisible();
288310
pointerRenderer.ToggleInteraction(currentPointerVisibility);
289311
}
290-
291312
CheckHoverSelect();
292313
}
293-
294-
UpdateDirectionIndicator();
295314
}
296315

297316
protected virtual void UpdateDirectionIndicator()
@@ -314,12 +333,12 @@ protected virtual void UpdateDirectionIndicator()
314333

315334
protected virtual bool EnabledPointerRenderer()
316335
{
317-
return (pointerRenderer && pointerRenderer.enabled);
336+
return (pointerRenderer != null && pointerRenderer.enabled);
318337
}
319338

320339
protected virtual bool NoPointerRenderer()
321340
{
322-
return (!pointerRenderer || !pointerRenderer.enabled);
341+
return (pointerRenderer == null || !pointerRenderer.enabled);
323342
}
324343

325344
protected virtual bool CanActivateOnToggleButton(bool state)
@@ -332,28 +351,35 @@ protected virtual bool CanActivateOnToggleButton(bool state)
332351
return result;
333352
}
334353

335-
protected virtual void FindController()
354+
protected virtual bool ControllerRequired()
355+
{
356+
return (activationButton != VRTK_ControllerEvents.ButtonAlias.Undefined || selectionButton != VRTK_ControllerEvents.ButtonAlias.Undefined);
357+
}
358+
359+
protected virtual bool FindController()
336360
{
337361
if (controller == null)
338362
{
339363
controller = GetComponentInParent<VRTK_ControllerEvents>();
340-
SetupController();
341364
}
342365

343-
if (controller == null && (activationButton != VRTK_ControllerEvents.ButtonAlias.Undefined || selectionButton != VRTK_ControllerEvents.ButtonAlias.Undefined))
366+
if (controller == null && ControllerRequired())
344367
{
345368
VRTK_Logger.Warn(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_Pointer", "VRTK_ControllerEvents", "the Controller Alias", ". To omit this warning, set the `Activation Button` and `Selection Button` to `Undefined`"));
369+
return false;
346370
}
347371

348372
if (directionIndicator != null)
349373
{
350374
directionIndicator.Initialize(controller);
351375
}
376+
377+
return true;
352378
}
353379

354380
protected virtual void SetupController()
355381
{
356-
if (controller)
382+
if (controller != null)
357383
{
358384
CheckButtonMappingConflict();
359385
SubscribeSelectionButton();
@@ -424,7 +450,7 @@ protected virtual void SubscribeActivationButton()
424450
UnsubscribeActivationButton();
425451
}
426452

427-
if (controller)
453+
if (controller != null)
428454
{
429455
controller.SubscribeToButtonAliasEvent(activationButton, true, DoActivationButtonPressed);
430456
controller.SubscribeToButtonAliasEvent(activationButton, false, DoActivationButtonReleased);
@@ -434,7 +460,7 @@ protected virtual void SubscribeActivationButton()
434460

435461
protected virtual void UnsubscribeActivationButton()
436462
{
437-
if (controller && subscribedActivationButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
463+
if (controller != null && subscribedActivationButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
438464
{
439465
controller.UnsubscribeToButtonAliasEvent(subscribedActivationButton, true, DoActivationButtonPressed);
440466
controller.UnsubscribeToButtonAliasEvent(subscribedActivationButton, false, DoActivationButtonReleased);
@@ -472,7 +498,7 @@ protected virtual void SubscribeSelectionButton()
472498
UnsubscribeSelectionButton();
473499
}
474500

475-
if (controller)
501+
if (controller != null)
476502
{
477503
controller.SubscribeToButtonAliasEvent(selectionButton, true, DoSelectionButtonPressed);
478504
controller.SubscribeToButtonAliasEvent(selectionButton, false, DoSelectionButtonReleased);
@@ -484,7 +510,7 @@ protected virtual void SubscribeSelectionButton()
484510

485511
protected virtual void UnsubscribeSelectionButton()
486512
{
487-
if (controller && subscribedSelectionButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
513+
if (controller != null && subscribedSelectionButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
488514
{
489515
controller.UnsubscribeToButtonAliasEvent(selectionButton, true, DoSelectionButtonPressed);
490516
controller.UnsubscribeToButtonAliasEvent(selectionButton, false, DoSelectionButtonReleased);
@@ -547,7 +573,7 @@ protected virtual void ManageActivationState(bool state)
547573

548574
protected virtual bool PointerActivatesUseAction(VRTK_InteractableObject givenInteractableObject)
549575
{
550-
return (givenInteractableObject && givenInteractableObject.pointerActivatesUseAction && givenInteractableObject.IsValidInteractableController(controller.gameObject, givenInteractableObject.allowedUseControllers));
576+
return (givenInteractableObject != null && givenInteractableObject.pointerActivatesUseAction && givenInteractableObject.IsValidInteractableController(controller.gameObject, givenInteractableObject.allowedUseControllers));
551577
}
552578

553579
protected virtual void StartUseAction(Transform target)
@@ -573,7 +599,7 @@ protected virtual void StopUseAction()
573599

574600
protected virtual void AttemptUseOnSet(Transform target)
575601
{
576-
if (pointerInteractableObject && target)
602+
if (pointerInteractableObject != null && target != null)
577603
{
578604
if (PointerActivatesUseAction(pointerInteractableObject))
579605
{

0 commit comments

Comments
 (0)