Skip to content

Instantly share code, notes, and snippets.

@mminer
Created June 7, 2021 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mminer/df80606774459d3327c848541082ac96 to your computer and use it in GitHub Desktop.
Save mminer/df80606774459d3327c848541082ac96 to your computer and use it in GitHub Desktop.
Unity XR component to move objects relative to their original position.
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class OffsetGrabInteractable : XRGrabInteractable
{
Vector3 interactorPosition;
Quaternion interactorRotation;
protected override void OnSelectEntered(XRBaseInteractor interactor)
{
base.OnSelectEntered(interactor);
interactorPosition = interactor.attachTransform.localPosition;
interactorRotation = interactor.attachTransform.localRotation;
var hasAttachTransform = attachTransform != null;
interactor.attachTransform.position = hasAttachTransform ? attachTransform.position : transform.position;
interactor.attachTransform.rotation = hasAttachTransform ? attachTransform.rotation : transform.rotation;
}
protected override void OnSelectExited(XRBaseInteractor interactor)
{
base.OnSelectExited(interactor);
interactor.attachTransform.localPosition = interactorPosition;
interactor.attachTransform.localRotation = interactorRotation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment