동굴재건
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Interactables;
|
||||
|
||||
public class SteeringKeyXR : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
[SerializeField] private UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable grabInteractable;
|
||||
[SerializeField] private XRGrabInteractable grabInteractable;
|
||||
[SerializeField] private Transform keyPivot;
|
||||
|
||||
[Header("Steering")]
|
||||
@@ -16,6 +17,7 @@ public class SteeringKeyXR : MonoBehaviour
|
||||
|
||||
private Transform currentInteractor;
|
||||
private bool isGrabbed;
|
||||
private bool warnedMissingGrabInteractable;
|
||||
|
||||
private Quaternion initialPivotRotation;
|
||||
private float currentAngle;
|
||||
@@ -24,6 +26,9 @@ public float SteeringValue
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Mathf.Approximately(maxAngle, 0f))
|
||||
return 0f;
|
||||
|
||||
return Mathf.Clamp(currentAngle / maxAngle, -1f, 1f);
|
||||
}
|
||||
}
|
||||
@@ -33,11 +38,14 @@ private void Awake()
|
||||
if (keyPivot == null)
|
||||
keyPivot = transform;
|
||||
|
||||
ResolveGrabInteractable();
|
||||
initialPivotRotation = keyPivot.localRotation;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
ResolveGrabInteractable();
|
||||
|
||||
if (grabInteractable != null)
|
||||
{
|
||||
grabInteractable.selectEntered.AddListener(OnGrab);
|
||||
@@ -82,7 +90,7 @@ private void OnRelease(SelectExitEventArgs args)
|
||||
private void UpdateAngleFromHand()
|
||||
{
|
||||
Vector3 worldDir = currentInteractor.position - keyPivot.position;
|
||||
Vector3 localDir = keyPivot.InverseTransformDirection(worldDir);
|
||||
Vector3 localDir = GetInitialPivotSpaceDirection(worldDir);
|
||||
|
||||
float targetAngle = Mathf.Atan2(localDir.x, localDir.z) * Mathf.Rad2Deg;
|
||||
|
||||
@@ -96,4 +104,27 @@ private void ApplyRotation()
|
||||
keyPivot.localRotation =
|
||||
initialPivotRotation * Quaternion.Euler(0f, currentAngle, 0f);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 GetInitialPivotSpaceDirection(Vector3 worldDirection)
|
||||
{
|
||||
Quaternion referenceRotation = keyPivot.parent != null
|
||||
? keyPivot.parent.rotation * initialPivotRotation
|
||||
: initialPivotRotation;
|
||||
|
||||
return Quaternion.Inverse(referenceRotation) * worldDirection;
|
||||
}
|
||||
|
||||
private void ResolveGrabInteractable()
|
||||
{
|
||||
if (grabInteractable != null)
|
||||
return;
|
||||
|
||||
grabInteractable = GetComponentInChildren<XRGrabInteractable>(true);
|
||||
|
||||
if (grabInteractable == null && !warnedMissingGrabInteractable)
|
||||
{
|
||||
warnedMissingGrabInteractable = true;
|
||||
Debug.LogWarning("[SteeringKeyXR] XRGrabInteractable 참조가 없어 키를 잡아도 조향 이벤트를 받을 수 없습니다.", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user