| | | 1 | | /* |
| | | 2 | | * Code adapted from: |
| | | 3 | | * https://medium.com/@dnwesdman/grab-offset-interactables-with-the-xr-interaction-toolkit-b2c18cec1a52 |
| | | 4 | | * by: |
| | | 5 | | * Dimitrios Vlachos |
| | | 6 | | * djv1@student.london.ac.uk |
| | | 7 | | * dimitri.j.vlachos@gmail.com |
| | | 8 | | */ |
| | | 9 | | using System.Collections; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using UnityEngine; |
| | | 12 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 13 | | |
| | | 14 | | public class OffsetInteractable : XRGrabInteractable |
| | | 15 | | { |
| | | 16 | | protected override void Awake() |
| | 6 | 17 | | { |
| | 6 | 18 | | base.Awake(); |
| | 6 | 19 | | CreateAttachTransform(); |
| | 6 | 20 | | } |
| | | 21 | | protected override void OnSelectEntering(SelectEnterEventArgs args) |
| | 0 | 22 | | { |
| | 0 | 23 | | base.OnSelectEntering(args); |
| | 0 | 24 | | MatchAttachPoint(args.interactorObject); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | protected void MatchAttachPoint(IXRInteractor interactor) |
| | 0 | 28 | | { |
| | 0 | 29 | | if (IsFirstSelecting(interactor)) |
| | 0 | 30 | | { |
| | 0 | 31 | | bool isDirect = interactor is XRDirectInteractor; |
| | 0 | 32 | | attachTransform.position = isDirect ? interactor.GetAttachTransform(this).position : transform.position; |
| | 0 | 33 | | attachTransform.rotation = isDirect ? interactor.GetAttachTransform(this).rotation : transform.rotation; |
| | 0 | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | private bool IsFirstSelecting(IXRInteractor interactor) |
| | 0 | 38 | | { |
| | 0 | 39 | | return interactor == firstInteractorSelecting; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | private void CreateAttachTransform() |
| | 6 | 43 | | { |
| | 6 | 44 | | if (attachTransform == null) |
| | 6 | 45 | | { |
| | 6 | 46 | | GameObject createdAttachTransform = new GameObject(); |
| | 6 | 47 | | createdAttachTransform.transform.parent = this.transform; |
| | 6 | 48 | | attachTransform = createdAttachTransform.transform; |
| | 6 | 49 | | } |
| | 6 | 50 | | } |
| | | 51 | | } |