| | | 1 | | /* |
| | | 2 | | * |
| | | 3 | | * Code by: |
| | | 4 | | * Dimitrios Vlachos |
| | | 5 | | * djv1@student.london.ac.uk |
| | | 6 | | * dimitri.j.vlachos@gmail.com |
| | | 7 | | * |
| | | 8 | | * References: |
| | | 9 | | * https://forum.unity.com/threads/how-to-get-which-hand-is-grabbing-an-xr-grab-interactable-object.946045/ |
| | | 10 | | */ |
| | | 11 | | |
| | | 12 | | using DynamicMeshCutter; |
| | | 13 | | using System.Collections; |
| | | 14 | | using System.Collections.Generic; |
| | | 15 | | using UnityEngine; |
| | | 16 | | using UnityEngine.InputSystem; |
| | | 17 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 18 | | |
| | | 19 | | [RequireComponent(typeof(XRGrabInteractable))] |
| | | 20 | | [RequireComponent(typeof(AudioSource))] |
| | | 21 | | public class KnifeBehaviour : MonoBehaviour |
| | | 22 | | { |
| | | 23 | | [Header("Cutting")] |
| | | 24 | | [SerializeField] private XRDirectInteractor rHand; |
| | | 25 | | [SerializeField] private XRDirectInteractor lHand; |
| | 2 | 26 | | [SerializeField] private InputActionReference right_cut_event = null; |
| | 2 | 27 | | [SerializeField] private InputActionReference left_cut_event = null; |
| | | 28 | | [SerializeField] private KnifePlaneBehaviour cutter; |
| | 2 | 29 | | [SerializeField] private float cutCooldownTimer = 0.5f; |
| | | 30 | | |
| | | 31 | | [Header("Knife Behaviour")] |
| | | 32 | | [SerializeField] private XRGrabInteractable grabInteractable; |
| | | 33 | | |
| | | 34 | | [Header("Sounds")] |
| | | 35 | | [SerializeField, Tooltip("List of cutting sounds to use")] |
| | | 36 | | List<AudioClip> cuttingSounds; |
| | | 37 | | [SerializeField] AudioSource audioSource; |
| | | 38 | | |
| | | 39 | | private Collider collider; |
| | | 40 | | private bool isSelected; |
| | | 41 | | |
| | | 42 | | private bool has_cut_recently; |
| | | 43 | | |
| | | 44 | | private void Awake() |
| | 1 | 45 | | { |
| | 1 | 46 | | right_cut_event.action.started += Right_cut; |
| | 1 | 47 | | left_cut_event.action.started += Left_cut; |
| | 1 | 48 | | } |
| | | 49 | | |
| | | 50 | | private void OnDestroy() |
| | 1 | 51 | | { |
| | 1 | 52 | | right_cut_event.action.started -= Right_cut; |
| | 1 | 53 | | left_cut_event.action.started -= Left_cut; |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | private void Start() |
| | 1 | 57 | | { |
| | 1 | 58 | | has_cut_recently = false; |
| | 1 | 59 | | grabInteractable = GetComponent<XRGrabInteractable>(); |
| | 1 | 60 | | collider = GetComponent<Collider>(); |
| | 1 | 61 | | isSelected = false; |
| | 1 | 62 | | audioSource = GetComponent<AudioSource>(); |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | private void Update() |
| | 1682 | 66 | | { |
| | | 67 | | // Check if object is grabbed, if so, then make collider a trigger |
| | 1682 | 68 | | if(grabInteractable.isSelected) |
| | 0 | 69 | | { |
| | 0 | 70 | | isSelected = true; |
| | 0 | 71 | | collider.isTrigger = true; |
| | 0 | 72 | | } |
| | 1682 | 73 | | else if(collider.isTrigger) |
| | 0 | 74 | | { |
| | 0 | 75 | | isSelected = false; |
| | 0 | 76 | | collider.isTrigger = false; |
| | 0 | 77 | | } |
| | 1682 | 78 | | } |
| | | 79 | | |
| | | 80 | | private void Left_cut(InputAction.CallbackContext context) |
| | 0 | 81 | | { |
| | 0 | 82 | | List<IXRSelectInteractable> interactables = lHand.interactablesSelected; |
| | | 83 | | |
| | 0 | 84 | | ConfirmValidCut(interactables); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | private void Right_cut(InputAction.CallbackContext context) |
| | 0 | 88 | | { |
| | 0 | 89 | | List<IXRSelectInteractable> interactables = rHand.interactablesSelected; |
| | | 90 | | |
| | 0 | 91 | | ConfirmValidCut(interactables); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | private void ConfirmValidCut(List<IXRSelectInteractable> interactables) |
| | 0 | 95 | | { |
| | 0 | 96 | | foreach (IXRSelectInteractable Interactable in interactables) |
| | 0 | 97 | | { |
| | 0 | 98 | | if (Interactable.transform.CompareTag("Knife")) |
| | 0 | 99 | | { |
| | 0 | 100 | | InitiateCut(); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public void InitiateCut() |
| | 7 | 106 | | { |
| | | 107 | | // Guard statements |
| | 7 | 108 | | if (!isSelected) |
| | 7 | 109 | | { |
| | 7 | 110 | | Debug.Log("Can't cut: not selected!"); |
| | 7 | 111 | | return; |
| | | 112 | | } |
| | 0 | 113 | | if (has_cut_recently) |
| | 0 | 114 | | { |
| | 0 | 115 | | Debug.Log("Can't cut: in cooldown!"); |
| | 0 | 116 | | return; |
| | | 117 | | } |
| | 0 | 118 | | Debug.Log("Attempting cut"); |
| | | 119 | | // Set the cut value to true |
| | 0 | 120 | | has_cut_recently = true; |
| | | 121 | | |
| | | 122 | | // Perform the cut |
| | 0 | 123 | | cutter.Cut(); |
| | 0 | 124 | | PlayCutSounds(); |
| | | 125 | | |
| | | 126 | | // Begin cut cooldown timer |
| | 0 | 127 | | StartCoroutine(ResetCutter()); |
| | 7 | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | IEnumerator ResetCutter() { |
| | | 131 | | // Wait for cooldown time |
| | 0 | 132 | | yield return new WaitForSeconds(cutCooldownTimer); |
| | 0 | 133 | | Debug.Log("Cooldown complete: cut ready"); |
| | 0 | 134 | | has_cut_recently = false; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | void PlayCutSounds() |
| | 0 | 138 | | { |
| | | 139 | | // Select a sound from the list at random |
| | 0 | 140 | | int randomSound = Random.Range(0, cuttingSounds.Count); |
| | | 141 | | // Pull that clip from the list |
| | 0 | 142 | | AudioClip clip = cuttingSounds[randomSound]; |
| | 0 | 143 | | audioSource.clip = clip; |
| | 0 | 144 | | audioSource.Play(); |
| | 0 | 145 | | } |
| | | 146 | | } |