Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My Rigidbody GameObject Ignores Colliders??

Discussion in 'Physics' started by shr7829bj, Oct 28, 2022.

  1. shr7829bj

    shr7829bj

    Joined:
    Sep 29, 2022
    Posts:
    2
    In my current game, I have an interactable block that the player can pick up and move around with. The block itself has a Rigidbody attatched and detects collisions perfectly in normal conditions.
    However, due to the way I make the pickup function (Which is simply by setting the parent of the block to the player so that it moves with the player), the block would ignore colliders when its picked up and can directly penetrate through walls and grounds.

    The question is: what do I need to do for the child block to not penetrate through other blocks when it's picked up? If that is not possible/ is very complicated, is there another approach I can do to pickup the block?

    To help you understand, here's my code for picking-up and releasing the block, as well as an image of the problem:

    Code (CSharp):
    1.         //PickupBlock
    2.         private void PickUp()
    3.         {
    4.             //Properties' changes
    5.             blockMoved = blockHit;
    6.             blockMoved.GetComponent<Rigidbody>().useGravity = false;
    7.             blockMoved.GetComponent<Rigidbody>().isKinematic = true;
    8.             movingBlock = true;
    9.  
    10.             //Move block
    11.             Vector3 blockPos = transform.position + transform.forward * 2.5f + transform.right * -0.5f;
    12.             blockMoved.transform.position = blockPos;
    13.             blockMoved.transform.parent = transform; //Set parent
    14.         }
    15.  
    16.         //ReleaseBlock
    17.         private void ReleaseBlock()
    18.         {
    19.             //Properties' changes
    20.             blockMoved.GetComponent<Rigidbody>().useGravity = true;
    21.             blockMoved.GetComponent<Rigidbody>().isKinematic = false;
    22.             movingBlock = false;
    23.  
    24.             //Move block
    25.             Vector3 blockPos = transform.position + transform.forward * 2.5f + transform.right * -0.5f;
    26.             blockMoved.transform.position = blockPos;
    27.             blockMoved.transform.parent = null; //Set parent
    28.  
    29.             blockMoved = null;
    30.  
    31.         }
    ProblemDemonstration.png
     

    Attached Files:

  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    You should make it non-kinematic.

    The simplest way to do it would be this:
    * Spawn an invisible rigidbody attached to player's hand, make it kinematic.
    * Attach rigidbody you carry to that kinematic body with a configurable joint
    * Make that joint use springs on all constraints.

    See:
    https://forum.unity.com/threads/vr-physical-interaction-test.1212024/
    https://forum.unity.com/threads/vr-physical-interaction-test.1212024/#post-7738326

    This is the easiest way to make it not pass through walls. It will press against walls, and move other rigidbodies while you carry it around.
     
  3. shr7829bj

    shr7829bj

    Joined:
    Sep 29, 2022
    Posts:
    2
    Thank you for replying!

    I tried your method, but was a bit stuck on the last step.
    What did you mean by using springs on all constraints?
    Can you be a bit more precise please? Sorry I never used this component before and don't understand its properties.
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    You need to play with Joints a bit to understand. The manual has articles about them.

    Basically a joint can be rigid, or it can be springy. If you use "springs" you'd be able to push linked bodies, and they'll bounce back when you release them. That's what you'd want in this case.

    If you can't do that with joints, you'd need to use some much more convoluted system with raycasts and all.