Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XRPushButton "doesn't work" if parent object is moved #108

Open
ddmck opened this issue Apr 8, 2023 · 1 comment
Open

XRPushButton "doesn't work" if parent object is moved #108

ddmck opened this issue Apr 8, 2023 · 1 comment

Comments

@ddmck
Copy link

ddmck commented Apr 8, 2023

When using the XRPushButton from within a parent gameobject that moves the button seems unresponsive. This is because in the OnStart function m_BaseButtonPosition is set, so the hover logic is basing its calculation on the initial position of the button, so it does "work" but only if you position your hand in the initial location.

I fixed my issue by updating

var localOffset = transform.InverseTransformVector(interactorTransform.position - m_BaseButtonPosition);

to

var localOffset = transform.InverseTransformVector(interactorTransform.position - m_Button.position);

in Unity.XR.Content.Interaction\Assets\XRI_Examples\UI_3D\Scripts\XRPushButton.cs

I realize this might incur performance penalties. I'm also pretty new to Unity so I might be misunderstanding things like the distance calculation mode.

Maybe a best of both worlds approach would be a toggle that enables the user to set the button as static or movable?

@josefnpat
Copy link

I ran into this exact issue and this is a good fix. Caching the button position means it won't work on moving parents.

@ddmck this seems like a reasonable fine fix.

Pinging @vrdave-unity with full diff that I used, if he wishes to upstream.

--- a/XRPushButton.cs
+++ b/XRPushButton.cs
@@ -59,7 +59,6 @@ namespace UnityEngine.XR.Content.Interaction
         bool m_Pressed = false;
         bool m_Toggled = false;
         float m_Value = 0f;
-        Vector3 m_BaseButtonPosition = Vector3.zero;

         Dictionary<IXRHoverInteractor, PressInfo> m_HoveringInteractors = new Dictionary<IXRHoverInteractor, PressInfo>();

@@ -128,12 +127,6 @@ namespace UnityEngine.XR.Content.Interaction
             return base.IsHoverableBy(interactor);
         }

-        void Start()
-        {
-            if (m_Button != null)
-                m_BaseButtonPosition = m_Button.position;
-        }
-
         protected override void OnEnable()
         {
             base.OnEnable();
@@ -196,7 +189,7 @@ namespace UnityEngine.XR.Content.Interaction
             foreach (var pressInfo in m_HoveringInteractors.Values)
             {
                 var interactorTransform = pressInfo.m_Interactor.GetAttachTransform(this);
-                var localOffset = transform.InverseTransformVector(interactorTransform.position - m_BaseButtonPosition);
+                var localOffset = transform.InverseTransformVector(interactorTransform.position - m_Button.position);

                 var withinButtonRegion = (Mathf.Abs(localOffset.x) < m_ButtonSize && Mathf.Abs(localOffset.z) < m_ButtonSize);
                 if (withinButtonRegion)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants