Search Unity

Resolved Grabbed Rigidbody is moving my XR Rig

Discussion in 'XR Interaction Toolkit and Input' started by PublicEnumE, Apr 4, 2021.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Hello! I have a strange problem, which I bet might be common:

    I have an XR Rig set up with Continuous Movement, and a Character Controller. I have a scene with a ground plane, full of RigidBodies which can be grabbed and dropped (using the default XR Rig grabbing behavior).

    The Problem is - Rigidbody objects that I've grabbed will interact physically with my XR Rig's Character controller.

    For example, if I grab and object and then move forward, the Character Controller will bump against the object I'm holding, causing both of us to jitter around. And if I take an Object I'm holding and just lower my hand, that will bump the grabbed object into my character controller, pushing me backwards.

    I want my XR rig to interact physically with objects that I'm not holding - like those which are already on the ground. And I want to be able to drop an object I'm holding and then be able to jump on top of it. So...

    What have people done to work around this? Is there an event I can listen for, that's raised when a Collider is "grabbed" by one of my controllers?

    I imagine that if there is, I can use that event to put the "held" object into another layer - one which doesn't interact physically with my character controller.

    Or - is there a built-in solution to this that I'm missing?

    Thank you for any help!
     
  2. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Solved this using the XRGrabInteractible.OnSelectEntered/Exited events.
     
    cmcosta81 likes this.
  3. NemesisWarlock

    NemesisWarlock

    Joined:
    Jan 21, 2017
    Posts:
    140
    Honestly, the way to really fix this is to simply put your character contrtoller's collider/rigidbody and your interactable objects on different layers, and then use the Physics Layer collision matrix to make sure that the interactables layer doesn't collide with the player controller layer.
     
    Rafaelm likes this.
  4. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    That only works if you don’t want your character controller to collide with interactable objects that you’re not currently carrying. For example, I want to be able to step onto objects after I’ve dropped them.

    So what I did was swap them to a separate, non-interacting layer only while they are being held.
     
  5. Lt-JP

    Lt-JP

    Joined:
    Oct 26, 2013
    Posts:
    1
    CPublicEnumE, can you share your solution?
     
  6. MagmaMc

    MagmaMc

    Joined:
    Apr 27, 2022
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.Interaction.Toolkit;
    3.  
    4. [RequireComponent(typeof(Collider))]
    5. [RequireComponent(typeof(XRGrabInteractable))]
    6. [DisallowMultipleComponent]
    7. public class GameObjectPickup : MonoBehaviour
    8. {
    9.     private XRGrabInteractable XRGrab;
    10.     private Collider[] Colliders;
    11.  
    12.     private void Start()
    13.     {
    14.         XRGrab = GetComponent<XRGrabInteractable>();
    15.         Colliders = GetComponents<Collider>();
    16.  
    17.         XRGrab.selectEntered.AddListener(Grab);
    18.         XRGrab.selectExited.AddListener(Drop);
    19.     }
    20.  
    21.     public void Grab(SelectEnterEventArgs args)
    22.     {
    23.         foreach (Collider collider in Colliders)
    24.             collider.isTrigger = true;
    25.     }
    26.  
    27.     public void Drop(SelectExitEventArgs args)
    28.     {
    29.         foreach (Collider collider in Colliders)
    30.             collider.isTrigger = false;
    31.  
    32.     }
    33. }
    34.  
     
  7. Keep-Frame

    Keep-Frame

    Joined:
    Sep 7, 2017
    Posts:
    5
    Hello, I've run into the same problem, but there is a solution.
    - I created a script IgnoreColliderObject

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IgnoreColliderObject : MonoBehaviour
    6. {
    7.     public Transform ObjectForIgnore;
    8.  
    9.     void Start()
    10.     {
    11.         Physics.IgnoreCollision(ObjectForIgnore.GetComponent<Collider>(), GetComponent<Collider>());
    12.     }
    13. }
    -Put it on the object that needs to ignore the collider
    - Select the XR Rig object and assign it to the ObjectForIgnore variable
    - In controller events, turn on and off the script when you throw an object on the ground
    (didn't check)