Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug ParentConstraint on an XR grab interactable when it's dropped

Discussion in 'Scripting' started by DishwashaBubble, Dec 10, 2023.

  1. DishwashaBubble

    DishwashaBubble

    Joined:
    Oct 19, 2023
    Posts:
    39
    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.)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.Interaction.Toolkit;
    5. using UnityEngine.Animations;
    6.  
    7. public class ToggleDishFreeze : MonoBehaviour
    8. {
    9.  
    10.     public XRGrabInteractable interactor = null;
    11.     public Rigidbody dishRigidBody;
    12.  
    13.     public ParentConstraint pConstraint;
    14.     public ConstraintSource cSource;
    15.     public int cSourceIndex;
    16.  
    17.     // 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.
    18.     public bool insideDishRack = false;
    19.  
    20.     public GameObject hitzone;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         interactor = GetComponent<XRGrabInteractable>();
    26.         interactor.selectEntered.AddListener(ResetKinematic);
    27.         interactor.selectExited.AddListener(ResetKinematicExit);
    28.  
    29.         dishRigidBody = GetComponent<Rigidbody>();
    30.  
    31.         pConstraint = GetComponent<ParentConstraint>();
    32.  
    33.     }
    34.  
    35.     void OnTriggerEnter(Collider other)
    36.     {
    37.         if (other.gameObject.name.Equals("hitzone"))
    38.         {
    39.             insideDishRack = true;
    40.  
    41.             cSource.sourceTransform = hitzone.transform;
    42.             cSourceIndex = pConstraint.AddSource(cSource);
    43.  
    44.             hitzone = other.gameObject;
    45.         }
    46.     }
    47.  
    48.     void OnTriggerExit(Collider other)
    49.     {
    50.         if (other.gameObject.name.Equals("hitzone"))
    51.         {
    52.             insideDishRack = false;
    53.             pConstraint.RemoveSource(cSourceIndex);
    54.         }
    55.     }
    56.  
    57.  
    58.     void ResetKinematic(SelectEnterEventArgs args)
    59.     {
    60.         // Don't need this atm / was part of my old implementation of just freezing the dish.
    61.  
    62.     }
    63.  
    64.     void ResetKinematicExit(SelectExitEventArgs args)
    65.     {
    66.         if (!insideDishRack)
    67.         {
    68.  
    69.         }
    70.         else
    71.         {
    72.  
    73.             pConstraint.SetTranslationOffset( cSourceIndex, hitzone.transform.position - transform.position );
    74.             pConstraint.SetRotationOffset( cSourceIndex, hitzone.transform.position - transform.position );
    75.             pConstraint.constraintActive = true;
    76.             pConstraint.locked = true;
    77.  
    78.         }
    79.     }
    80.  
    81. }
    82.  
     
  2. DishwashaBubble

    DishwashaBubble

    Joined:
    Oct 19, 2023
    Posts:
    39
    Anyone got advice?
     
  3. DishwashaBubble

    DishwashaBubble

    Joined:
    Oct 19, 2023
    Posts:
    39
    I still need help on this please. Thanks.
     
  4. mrphilipjoel

    mrphilipjoel

    Joined:
    Jul 6, 2019
    Posts:
    62
    Make your dish rack have a large trigger. When enter trigger, have some bool that is set to true 'in dish rack'. On exit, set it to false.

    On the grab interactable 'Interactable Events', go to the 'Selected Exit' event. When that event is triggered, check your bool mentioned before. If true, set parent to the dish rack.
     
  5. DishwashaBubble

    DishwashaBubble

    Joined:
    Oct 19, 2023
    Posts:
    39
    I ended up just using joints, but thank you.