From a3a27320607fdd1519c122ad22ce18d460382328 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 8 Feb 2024 15:38:52 +0100 Subject: [PATCH] Prevent ghost clicks using finger interaction --- Runtime/Tools/Scripts/InteractionModule.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Runtime/Tools/Scripts/InteractionModule.cs b/Runtime/Tools/Scripts/InteractionModule.cs index 7e94c32..1c818ea 100644 --- a/Runtime/Tools/Scripts/InteractionModule.cs +++ b/Runtime/Tools/Scripts/InteractionModule.cs @@ -528,18 +528,15 @@ namespace Passer { if (Camera.main == null || pointer.type != PointerType.Touch) return; - //if (eventSystem == null) - // eventSystem = Object.FindObjectOfType(); - //if (eventSystem == null) - // return; - pointer.data.position = Camera.main.WorldToScreenPoint(pointer.pointerTransform.position); + float distanceToPointer = Vector3.Distance(Camera.main.transform.position, pointer.pointerTransform.position); System.Collections.Generic.List m_RaycastResultCache = new System.Collections.Generic.List(); eventSystem.RaycastAll(pointer.data, m_RaycastResultCache); - RaycastResult raycastResult = FindFirstRaycast(m_RaycastResultCache); + RaycastResult raycastResult = FindFirstRaycast(m_RaycastResultCache, distanceToPointer); m_RaycastResultCache.Clear(); + if (raycastResult.gameObject != null) { if (pointer.type == PointerType.Touch) { float distance = DistanceTipToTransform(pointer.pointerTransform, raycastResult.gameObject.transform); @@ -559,9 +556,12 @@ namespace Passer { } } - private new RaycastResult FindFirstRaycast(System.Collections.Generic.List raycastResults) { + private RaycastResult FindFirstRaycast(System.Collections.Generic.List raycastResults, float pointerDistance) { foreach (RaycastResult result in raycastResults) { - if (result.isValid && result.worldPosition != Vector3.zero) { + float resultDistance = Vector3.Distance(Camera.main.transform.position, result.worldPosition); + if (result.isValid && + result.worldPosition != Vector3.zero && + Mathf.Abs(resultDistance - pointerDistance) < 0.02F) { return result; } } @@ -580,7 +580,6 @@ namespace Passer { } private float DistanceTipToTransform(Transform fingerTip, Transform transform) { - //Debug.DrawLine(fingerTip.position, transform.position); return (-transform.InverseTransformPoint(fingerTip.position).z * transform.lossyScale.z) - 0.01F; } }