Hi,
I’m continuing work on my dishwashing VR game. I want to place a dish (xr grabbable) on the dish rack (hitzone) and have it lock in place via a ParentConstraint. I’ve tried implementing it with little success. Whenever I place the dish in the rack it just pops out with seemingly no parent constraint active.
(Mods please don’t delete my thread; this is the most relevant category that I have permission to post in. I can’t post in the XR discussion and this is moreso related to scripting.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.Animations;
public class ToggleDishFreeze : MonoBehaviour
{
public XRGrabInteractable interactor = null;
public Rigidbody dishRigidBody;
public ParentConstraint pConstraint;
public ConstraintSource cSource;
public int cSourceIndex;
// Keep track because, now that we're unfreezing on grab exit, we need to account for the case when we're releasing inside the rack and not unfreeze.
public bool insideDishRack = false;
public GameObject hitzone;
// Start is called before the first frame update
void Start()
{
interactor = GetComponent<XRGrabInteractable>();
interactor.selectEntered.AddListener(ResetKinematic);
interactor.selectExited.AddListener(ResetKinematicExit);
dishRigidBody = GetComponent<Rigidbody>();
pConstraint = GetComponent<ParentConstraint>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name.Equals("hitzone"))
{
insideDishRack = true;
cSource.sourceTransform = hitzone.transform;
cSourceIndex = pConstraint.AddSource(cSource);
hitzone = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.name.Equals("hitzone"))
{
insideDishRack = false;
pConstraint.RemoveSource(cSourceIndex);
}
}
void ResetKinematic(SelectEnterEventArgs args)
{
// Don't need this atm / was part of my old implementation of just freezing the dish.
}
void ResetKinematicExit(SelectExitEventArgs args)
{
if (!insideDishRack)
{
}
else
{
pConstraint.SetTranslationOffset( cSourceIndex, hitzone.transform.position - transform.position );
pConstraint.SetRotationOffset( cSourceIndex, hitzone.transform.position - transform.position );
pConstraint.constraintActive = true;
pConstraint.locked = true;
}
}
}