Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created May 7, 2024 12:21
Show Gist options
  • Save unitycoder/626cd3fddeb0f8d18b2b946c236673ce to your computer and use it in GitHub Desktop.
Save unitycoder/626cd3fddeb0f8d18b2b946c236673ce to your computer and use it in GitHub Desktop.
Reset Draggable object, unless its placed on socket
using System.Collections;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class ResetDraggable : MonoBehaviour
{
XRGrabInteractable grabInteractable;
Vector3 origPos;
Quaternion origRot;
void Start()
{
origPos = transform.position;
origRot = transform.rotation;
grabInteractable = GetComponent<XRGrabInteractable>();
grabInteractable.selectEntered.AddListener(OnSelectEntered);
grabInteractable.selectExited.AddListener(OnSelectExited);
}
// this object dropped
private void OnSelectExited(SelectExitEventArgs arg0)
{
// check if connected to socket or not
Debug.Log(arg0.interactorObject.transform.name + " dropped " + gameObject.name);
StopAllCoroutines();
StartCoroutine(ResetSoon());
}
IEnumerator ResetSoon()
{
yield return new WaitForSeconds(1);
Reset();
}
// this object picked up
private void OnSelectEntered(SelectEnterEventArgs arg0)
{
StopAllCoroutines();
Debug.Log(arg0.interactorObject.transform.name + " picked up " + gameObject.name);
}
public void Reset()
{
// TODO draw line from current position to original position, or show other fx
transform.position = origPos;
transform.rotation = origRot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment