diff --git a/Assets/02_Scripts/Shopping/BarcodeScaner.cs b/Assets/02_Scripts/Shopping/BarcodeScaner.cs index b4c74258..022a533d 100644 --- a/Assets/02_Scripts/Shopping/BarcodeScaner.cs +++ b/Assets/02_Scripts/Shopping/BarcodeScaner.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using UnityEngine; using VRShopping.Interact; using VRShopping.Items; @@ -18,9 +17,8 @@ public class BarcodeScaner : MonoBehaviour [SerializeField] private CheckoutMachine _checkoutMachine; private readonly Collider[] _overlapBuffer = new Collider[32]; - private readonly HashSet _scannedBuffer = new(); - // 스캔영역 콜라이더 안에 들어온 아이템들을 스캔함 + // 스캔영역 콜라이더 안에서 중심에 가장 가까운 아이템 하나만 스캔 public void ScanArea() { if (_scanAreaCollider == null) return; @@ -28,12 +26,24 @@ public void ScanArea() var bounds = _scanAreaCollider.bounds; var count = Physics.OverlapBoxNonAlloc(bounds.center, bounds.extents, _overlapBuffer, Quaternion.identity); - _scannedBuffer.Clear(); + ItemInstance closest = null; + float closestDistSqr = float.MaxValue; + var center = bounds.center; + for (int i = 0; i < count; i++) { var item = _overlapBuffer[i].GetComponentInParent(); - if (item != null && _scannedBuffer.Add(item)) ScanProduction(item); + if (item == null) continue; + + float d = (item.transform.position - center).sqrMagnitude; + if (d < closestDistSqr) + { + closestDistSqr = d; + closest = item; + } } + + if (closest != null) ScanProduction(closest); } //ItemInstance의 상품 정보를 스캔