using jeanf.EventSystem; using jeanf.validationTools; using UnityEngine; using UnityEngine.XR.Interaction.Toolkit.Interactors; namespace jeanf.universalplayer { public class HandsPhysics : MonoBehaviour { [Validation("The tracked target the physics hand follows is required — its position/rotation are read every Update/FixedUpdate unguarded (a null reference throws).")] [SerializeField] private Transform target; private Rigidbody rb; [SerializeField] private Quaternion offset; [Validation("The non-physical (ghost) hand is required — it is toggled every Update unguarded (a null reference throws).")] [SerializeField] private GameObject nonPhysicalHand; [SerializeField] private float showNonPhysicalHandDistance = 0.5f; [Header("Divergence ghost look")] [Tooltip("Translucent material for the non-physical hand under URP / Built-in (packaged: GhostHands_URP). Applied to every renderer under Non Physical Hand at start; empty keeps the hand's own look.")] [SerializeField] private Material ghostMaterialUrp; [Tooltip("Translucent material for the non-physical hand under HDRP (packaged: GhostHands, an HD Unlit graph). The active pipeline picks between the two automatically.")] [SerializeField] private Material ghostMaterialHdrp; Collider[] handColliders; [SerializeField] LayerMask ignoreTheseOnGrab; GameObject pokeInteractor; [SerializeField] HandType handType; [Validation("The control-scheme change channel is required — subscribed unguarded at startup (a null reference throws); the physics hands never follow XR on/off without it.")] [SerializeField] VoidEventChannelSO controlSchemeChangeEvent; void Start() { rb = GetComponent(); handColliders = GetComponentsInChildren(); pokeInteractor = GetComponentInChildren().gameObject; // The ghost is an indicator (the real hand kept going while the physics hand was // held back): give it the translucent look authored for the running pipeline. PipelineMaterials.ApplyToRenderers(nonPhysicalHand, PipelineMaterials.Pick(ghostMaterialUrp, ghostMaterialHdrp)); CheckXRStatus(); } private void OnEnable() { TakeObject.OnGrabDeactivateCollider += HandleColliders; GetPrimaryInHandItemWithVRController.OnIpadStateChanged += HandleCollidersForSpecificHand; controlSchemeChangeEvent.OnEventRaised += CheckXRStatus; PrimaryItemController.TriggerLastUsedHand += HandleColliders; } private void OnDisable() => Unsubscribe(); private void OnDestroy() => Unsubscribe(); private void Unsubscribe() { TakeObject.OnGrabDeactivateCollider -= HandleColliders; GetPrimaryInHandItemWithVRController.OnIpadStateChanged -= HandleCollidersForSpecificHand; controlSchemeChangeEvent.OnEventRaised -= CheckXRStatus; PrimaryItemController.TriggerLastUsedHand -= HandleColliders; } private void Update() { float distance = Vector3.Distance(transform.position, target.position); if (distance > showNonPhysicalHandDistance) { nonPhysicalHand.SetActive(true); } else { nonPhysicalHand.SetActive(false); } } void FixedUpdate() { rb.linearVelocity = (target.position - transform.position) / Time.fixedDeltaTime; Quaternion rotationDifference = target.rotation * Quaternion.Inverse(transform.rotation * offset); rotationDifference.ToAngleAxis(out float angleInDegree, out Vector3 rotationAxis); Vector3 rotationDifferenceInDegree = angleInDegree * rotationAxis; rb.angularVelocity = (rotationDifferenceInDegree * Mathf.Deg2Rad / Time.deltaTime); } private void CheckXRStatus() { if (BroadcastControlsStatus.controlScheme == BroadcastControlsStatus.ControlScheme.XR) { transform.parent.gameObject.SetActive(true); } else { transform.parent.gameObject.SetActive(false); } } private void HandleCollidersForSpecificHand(IpadState value) { switch (value) { case IpadState.Disabled: pokeInteractor.SetActive(true); foreach (Collider collider in handColliders) { collider.excludeLayers = 0; } break; case IpadState.InLeftHand: if (handType == HandType.Left) { foreach (Collider collider in handColliders) { collider.excludeLayers = ignoreTheseOnGrab; pokeInteractor.SetActive(false); } } else { pokeInteractor.SetActive(true); foreach (Collider collider in handColliders) { collider.excludeLayers = 0; } } break; case IpadState.InRightHand: if (handType == HandType.Right) { foreach (Collider collider in handColliders) { collider.excludeLayers = ignoreTheseOnGrab; pokeInteractor.SetActive(false); } } else { pokeInteractor.SetActive(true); foreach (Collider collider in handColliders) { collider.excludeLayers = 0; } } break; } } private void HandleColliders(bool value, HandType side) { if (side == handType) { if (value) { foreach (Collider collider in handColliders) { collider.excludeLayers = ignoreTheseOnGrab; } } else { foreach (Collider collider in handColliders) { collider.excludeLayers = 0; } } } } private void HandleColliders(XRHandsInteractionManager.LastUsedHand hand, bool state) { switch (hand) { case XRHandsInteractionManager.LastUsedHand.LeftHand: if (handType != HandType.Right) return; if (state) { foreach (Collider collider in handColliders) { collider.excludeLayers = ignoreTheseOnGrab; } } else { foreach (Collider collider in handColliders) { collider.excludeLayers = 0; } } break; case XRHandsInteractionManager.LastUsedHand.RightHand: if (handType != HandType.Left) return; if (state) { foreach (Collider collider in handColliders) { collider.excludeLayers = ignoreTheseOnGrab; } } else { foreach (Collider collider in handColliders) { collider.excludeLayers = 0; } } break; } } } }