Stratasys-450mc-VR/Assets/Scripts/Interactions/LockingInteractable.cs

44 lines
1.1 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
namespace Interactions
{
[DisallowMultipleComponent]
[RequireComponent(typeof(XRGrabInteractable))]
public class LockingInteractable : MonoBehaviour
{
[SerializeField] private InteractionLayerMask unlockedLayer;
[SerializeField] private bool isLocked = false;
private XRGrabInteractable grabInteractable;
public bool IsLocked => isLocked;
private void OnEnable()
{
TryGetComponent(out grabInteractable);
if (isLocked)
{
Lock();
return;
}
Unlock();
}
public void Unlock()
{
// Add the unlocked layer to the interaction layer mask
grabInteractable.interactionLayerMask |= unlockedLayer;
isLocked = false;
}
public void Lock()
{
// Remove the unlocked layer from the interaction layer mask
grabInteractable.interactionLayerMask &= ~unlockedLayer;
isLocked = true;
}
}
}