2026-04-20 카트 잡기
This commit is contained in:
@@ -1,10 +1,33 @@
|
||||
using UnityEngine;
|
||||
using VRShopping.UI;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
private Animator _anim;
|
||||
|
||||
//나중에 stateMachine으로 변경
|
||||
private bool _controlPositive = true;
|
||||
|
||||
[SerializeField] private ShoppingOrderView _shoppingOrderView;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_anim = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
//InputManager.Instance.XRLeftControllerPrimaryButton_Event += 장보기 목록 온오프 함수
|
||||
InputManager.Instance.XRLeftControllerPrimaryButton_Event += this.ToggleShoppingOrderList;
|
||||
}
|
||||
|
||||
public async void ToggleShoppingOrderList()
|
||||
{
|
||||
if(!_controlPositive) return;
|
||||
|
||||
Debug.Log("ToggleShoppingOrderList");
|
||||
|
||||
_controlPositive = false;
|
||||
await _shoppingOrderView.ToggleShoppingOrderList();
|
||||
_controlPositive = true;
|
||||
}
|
||||
}
|
||||
|
||||
123
Assets/02_Scripts/Player/RideController.cs
Normal file
123
Assets/02_Scripts/Player/RideController.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Interactables;
|
||||
|
||||
public class RideController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform _itemRoot;
|
||||
[SerializeField, Min(0f)] private float _settleVelocity = 0.1f;
|
||||
[SerializeField, Min(0f)] private float _settleDuration = 0.3f;
|
||||
|
||||
private ParentConstraint _parentConstraint;
|
||||
private readonly Dictionary<Rigidbody, float> _settleTimer = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_parentConstraint = GetComponent<ParentConstraint>();
|
||||
|
||||
if (_parentConstraint.sourceCount == 0 ||
|
||||
_parentConstraint.GetSource(0).sourceTransform == null)
|
||||
{
|
||||
var xrOrigin = FindFirstObjectByType<XROrigin>();
|
||||
if (xrOrigin == null)
|
||||
{
|
||||
Debug.LogWarning("RideController: 씬에서 XROrigin을 찾지 못했습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
var source = new ConstraintSource
|
||||
{
|
||||
sourceTransform = xrOrigin.transform,
|
||||
weight = 1f
|
||||
};
|
||||
|
||||
if (_parentConstraint.sourceCount == 0)
|
||||
_parentConstraint.AddSource(source);
|
||||
else
|
||||
_parentConstraint.SetSource(0, source);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void ActiveRide()
|
||||
{
|
||||
_parentConstraint.constraintActive = true;
|
||||
}
|
||||
|
||||
public void DeactiveRide()
|
||||
{
|
||||
_parentConstraint.constraintActive = false;
|
||||
}
|
||||
|
||||
public void OnDetectionStay(Collider other)
|
||||
{
|
||||
var rb = other.attachedRigidbody;
|
||||
if (rb == null || rb.isKinematic) return;
|
||||
if (!rb.TryGetComponent(out XRGrabInteractable grab)) return;
|
||||
|
||||
if (grab.isSelected)
|
||||
{
|
||||
_settleTimer.Remove(rb);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rb.linearVelocity.magnitude > _settleVelocity)
|
||||
{
|
||||
_settleTimer[rb] = 0f;
|
||||
return;
|
||||
}
|
||||
|
||||
float elapsed = _settleTimer.GetValueOrDefault(rb, 0f) + Time.deltaTime;
|
||||
if (elapsed >= _settleDuration)
|
||||
{
|
||||
AttachToCart(grab, rb);
|
||||
_settleTimer.Remove(rb);
|
||||
}
|
||||
else
|
||||
{
|
||||
_settleTimer[rb] = elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDetectionExit(Collider other)
|
||||
{
|
||||
var rb = other.attachedRigidbody;
|
||||
if (rb != null) _settleTimer.Remove(rb);
|
||||
}
|
||||
|
||||
private void AttachToCart(XRGrabInteractable grab, Rigidbody rb)
|
||||
{
|
||||
grab.transform.SetParent(_itemRoot);
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.angularVelocity = Vector3.zero;
|
||||
rb.isKinematic = true;
|
||||
|
||||
// 잡았다 놓을 때 XRGrabInteractable이 parent를 ItemRoot로 되돌리는 것 방지
|
||||
grab.retainTransformParent = false;
|
||||
|
||||
grab.selectEntered.AddListener(OnPickedUpFromCart);
|
||||
}
|
||||
|
||||
private void OnPickedUpFromCart(SelectEnterEventArgs args)
|
||||
{
|
||||
if (args.interactableObject is not XRGrabInteractable grab) return;
|
||||
|
||||
grab.transform.SetParent(null);
|
||||
|
||||
grab.selectEntered.RemoveListener(OnPickedUpFromCart);
|
||||
grab.selectExited.AddListener(OnReleasedAfterPickup);
|
||||
}
|
||||
|
||||
private void OnReleasedAfterPickup(SelectExitEventArgs args)
|
||||
{
|
||||
if (args.interactableObject is not XRGrabInteractable grab) return;
|
||||
|
||||
// XRGrabInteractable이 original(kinematic=true)로 복원한 것을 덮어씀
|
||||
if (grab.TryGetComponent(out Rigidbody rb)) rb.isKinematic = false;
|
||||
|
||||
grab.selectExited.RemoveListener(OnReleasedAfterPickup);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Player/RideController.cs.meta
Normal file
2
Assets/02_Scripts/Player/RideController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a30bf3ffde1d9f458179bc3de7798bc
|
||||
16
Assets/02_Scripts/Player/RideDetectionZone.cs
Normal file
16
Assets/02_Scripts/Player/RideDetectionZone.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class RideDetectionZone : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private RideController _ride;
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
if (_ride != null) _ride.OnDetectionStay(other);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (_ride != null) _ride.OnDetectionExit(other);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Player/RideDetectionZone.cs.meta
Normal file
2
Assets/02_Scripts/Player/RideDetectionZone.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6318bd3f56cf2c49813bcb2d813a7ec
|
||||
Reference in New Issue
Block a user