-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVRKnife.cs
More file actions
51 lines (44 loc) · 1.51 KB
/
Copy pathVRKnife.cs
File metadata and controls
51 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR.Interaction.Toolkit.Interactables;
using UnityEngine.XR.Interaction.Toolkit.Interactors;
[RequireComponent(typeof(Rigidbody))]
public class VRKnife : MonoBehaviour
{
[Header("Cutting Settings")]
public float cutRadius = 0.1f;
public float cutForce = -0.5f;
[Header("Haptics")]
[Tooltip("Reference to the Grab Interactable on this object.")]
public XRGrabInteractable grabInteractable;
public float hapticIntensity = 0.5f;
public float hapticDuration = 0.1f;
private void OnValidate()
{
if (grabInteractable == null)
{
grabInteractable = GetComponent<XRGrabInteractable>();
}
}
private void OnCollisionStay(Collision collision)
{
MeshDeformer deformer = collision.gameObject.GetComponent<MeshDeformer>();
if (deformer != null)
{
ContactPoint contact = collision.GetContact(0);
deformer.AddDeformation(contact.point, cutRadius, cutForce);
TriggerHaptic();
}
}
private void TriggerHaptic()
{
if (grabInteractable != null && grabInteractable.isSelected)
{
var interactor = grabInteractable.interactorsSelecting[0];
if (interactor is XRBaseInputInteractor inputInteractor)
{
inputInteractor.SendHapticImpulse(hapticIntensity, hapticDuration);
}
}
}
}