Files
DeltaVR/Assets/_PROJECT/Models/DoorHandReplacer.cs

147 lines
3.9 KiB
C#

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<SmartHandPresence> handsInTrigger = new HashSet<SmartHandPresence>();
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<Animator>();
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<HandPresencePhysics>() != 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<SmartHandPresence>();
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<SmartHandPresence>();
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;
}
}
}
}