Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Is Rigidbody.detectCollisions meant to work this way?

Discussion in 'Physics' started by SacredSticks, Dec 13, 2022.

  1. SacredSticks


    Dec 15, 2020
    Let me explain.
    I was looking through code from a package I bought from the asset store because I was trying to understand it so that I could create a similar effect in my own scripts where I don't need the package. This script was meant to go onto an object and then this object could be stabbed into other GameObjects, ignoring the colliders (it's a VR package).

    I looked over the code and I found the spot that's doing the important part, so I made my own very much in progress script that has the same effect, but I'm not sure if it's intentional or not. I'm mainly worried that if this isn't the way that the code is meant to work, then it would likely eventually get fixed and my script wouldn't work anymore. Anyways here's the code...

    private void StabEnter(GameObject stab)
    if (stab.GetComponent<Rigidbody>() == null)
    joint = gameObject.AddComponent<ConfigurableJoint>();
    joint.connectedBody = stab.GetComponent<Rigidbody>();
    body.detectCollisions = false;
    body.detectCollisions = true;

    The main thing that's setting me off about this is that it's setting detectCollisions to false and then immediately to true again afterwards. It works, it absolutely does stop the collision from happening and allows the GameObject to be within the other.

    Edit: I'm trying to add a configurable joint to this and use limited motion to only let the stabber go a certain distance, but it's ignoring the linear limit (and yes I did set motion to be locked and limited motion appropriately). This is an entirely separate issue or question but if anybody knows how I could solve this I would appreciate that as well.
    Last edited: Dec 13, 2022