using _PROJECT.NewHandPresence; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public enum GrabbingHand { Right, Left } public class DoorHandReplacer : MonoBehaviour { public GameObject LeftHand; public GameObject RightHand; public GameObject DoorSlab; public GameObject handle; public float DoorClosedTolerance = 5f; private GrabbingHand? hand = null; // nullable private SmartHandPresence currentHand = null; private bool isGrabbing; // Tracks all hands currently inside the trigger private HashSet handsInTrigger = new HashSet(); public void ManifestDoorHand() { if (currentHand == null || hand == null) return; isGrabbing = true; switch (hand.Value) { case GrabbingHand.Left: RightHand.SetActive(false); LeftHand.SetActive(true); Debug.Log("Dissapearing hand"); break; case GrabbingHand.Right: LeftHand.SetActive(false); RightHand.SetActive(true); Debug.Log("Dissapearing hand"); break; } float doorSlabRotation = DoorSlab.transform.localRotation.y; if (Mathf.Abs(doorSlabRotation) < Mathf.Abs(DoorClosedTolerance)) // If door is closed { Animator animator = handle.GetComponent(); if (animator != null) { animator.SetTrigger("TurnHandle"); } } } public void DeManifestDoorHand() { isGrabbing = false; LeftHand.SetActive(false); RightHand.SetActive(false); if (currentHand == null) return; } private void OnTriggerEnter(Collider other) { if (isGrabbing && other.GetComponentInParent() != null) { if (other.gameObject.name.Contains("left", System.StringComparison.OrdinalIgnoreCase)) { if (hand == GrabbingHand.Left) DeManifestDoorHand(); isGrabbing = false; return; } else if (other.gameObject.name.Contains("right", System.StringComparison.OrdinalIgnoreCase)) { if (hand == GrabbingHand.Right) DeManifestDoorHand(); isGrabbing = false; return; } } if (isGrabbing) return; // IMPORTANT: in case hands have multiple colliders SmartHandPresence playerHand = other.GetComponentInParent(); if (playerHand == null) return; handsInTrigger.Add(playerHand); currentHand = playerHand; string name = playerHand.gameObject.name; if (name.Contains("left", System.StringComparison.OrdinalIgnoreCase)) { hand = GrabbingHand.Left; } else if (name.Contains("right", System.StringComparison.OrdinalIgnoreCase)) { hand = GrabbingHand.Right; } } private void OnTriggerExit(Collider other) { if (isGrabbing) return; SmartHandPresence playerHand = other.GetComponentInParent(); if (playerHand == null) return; handsInTrigger.Remove(playerHand); // If no hands remain, clear everything if (handsInTrigger.Count == 0) { currentHand = null; hand = null; } else { // Pick any remaining hand as current foreach (var remainingHand in handsInTrigger) { currentHand = remainingHand; string name = remainingHand.gameObject.name; hand = name.Contains("left", System.StringComparison.OrdinalIgnoreCase) ? GrabbingHand.Left : GrabbingHand.Right; break; } } } }