Skip to content

Instantly share code, notes, and snippets.

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 thestonefox/f604c6e1681117752ca1ac633f30656e to your computer and use it in GitHub Desktop.
Save thestonefox/f604c6e1681117752ca1ac633f30656e to your computer and use it in GitHub Desktop.
Tilia Interactables Example of how to get two handed grabbing to work with a precision grabbed object so either hand can be holding it
using System.Collections;
using Tilia.Interactions.Interactables.Interactables;
using Tilia.Interactions.Interactables.Interactables.Grab.Provider;
using UnityEngine;
using Zinnia.Data.Collection.Stack;
public class PrecisionFollowPrimarySecondarySwapper : MonoBehaviour
{
[SerializeField]
private InteractableFacade interactable;
public InteractableFacade Interactable
{
get { return interactable; }
set
{
UnregisterListener(interactable);
interactable = value;
RegisterListener(interactable);
}
}
[SerializeField]
private int stackIndex;
public int StackIndex
{
get { return stackIndex; }
set { stackIndex = value; }
}
protected Coroutine swapRoutine;
protected virtual void OnEnable()
{
RegisterListener(Interactable);
}
protected virtual void OnDisable()
{
StopSwapRoutine();
UnregisterListener(Interactable);
}
protected virtual void RegisterListener(InteractableFacade data)
{
GameObjectObservableStack stack = GetStack(data);
if (stack == null)
{
return;
}
stack.ElementEvents[1].ForcePopped.AddListener(Swap);
}
protected virtual void UnregisterListener(InteractableFacade data)
{
GameObjectObservableStack stack = GetStack(data);
if (stack == null)
{
return;
}
stack.ElementEvents[1].ForcePopped.RemoveListener(Swap);
}
protected virtual GameObjectObservableStack GetStack(InteractableFacade input)
{
if (input == null)
{
return null;
}
GrabInteractableStackInteractorProvider provider = (GrabInteractableStackInteractorProvider)input.Configuration.GrabConfiguration.GrabProviderOptions[StackIndex];
return provider != null && provider.EventStack.ElementEvents.Count > 1 ? provider.EventStack : null;
}
protected virtual void Swap(GameObject interactor)
{
StopSwapRoutine();
swapRoutine = StartCoroutine(SwapAtEndOfFrame(interactor));
}
protected virtual void StopSwapRoutine()
{
if (swapRoutine != null)
{
StopCoroutine(swapRoutine);
swapRoutine = null;
}
}
protected virtual IEnumerator SwapAtEndOfFrame(GameObject interactor)
{
yield return new WaitForEndOfFrame();
Interactable.Grab(interactor);
swapRoutine = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment