Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

DistanceJoint2D disables collision detection

Discussion in 'Physics' started by Krijojo, Dec 1, 2019 at 5:56 PM.

  1. Krijojo

    Krijojo

    Joined:
    Sunday
    Posts:
    3
    Hello, everyone! Maybe this is a simple problem to be solved, but I am a beginner, and didn't manage to find any solution to it.
    I have a dynamic Rigidbody2D as a character. I need to allow the player to hang to another gameobject (which has a kinematic Rigidbody2D) with a ray, and let him swing. The ray is rigid, it does not behave like a rope.
    I managed to write a script to make the character hang and swing, however, it seems that when the character gameobject has the DistanceJoint2D component, it does not detect any collision.

    I'll try to explain better with an example:
    if I walk normally and I go against an obstacle (which has a capsule collider, with isTrigger property set to false) the character is blocked, and can't go on. Essentially, it walks in place.
    When I hit the same obstacle while I'm swinging, instead, the character goes beyond it.
    I'd like the character to be stopped by the obstacle also when it is swinging.

    This is the code snippet I use to create the joint:
    Code (CSharp):
    1. _joint = gameObject.AddComponent<DistanceJoint2D>();
    2. _friction = gameObject.AddComponent<FrictionJoint2D>();
    3. Rigidbody2D otherRb = _selectedStar.GetComponent<Rigidbody2D>();
    4.  _joint.distance = (_position - otherRb.position).magnitude;
    5. _joint.maxDistanceOnly = _joint.distance < _minHangDistance;
    6. _joint.connectedBody = otherRb;
    7. _joint.autoConfigureDistance = false;
    8. _joint.enableCollision = true;
    9.  _friction.maxForce = 1;
    10.  hangingEffect.StartEffect(otherRb.transform);
    The FrictionJoint2D is used simply to add some friction to the character in order to make it slowly stop if the player is not pressing any key.

    Is there any way I can enable collision detection even with the DistanceJoint2D component enabled?
     
  2. Krijojo

    Krijojo

    Joined:
    Sunday
    Posts:
    3
    I made some tests and I found out that it is not the DistanceJoint2D component which disables collision detection, but it seems to be the FrictionJoint2D, instead.
    Indeed, if I don't add it to my character, it correctly collides against the obstacle, without going beyond it.
    Is there any reason why this should happen?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,048
    Maybe Joint2D.enableCollision is off? If the connected body of the joint isn't specified then it'll implicitly be the default static ground-body. In that situation with this option off it'll stop the Rigidbody2D this joint is on colliding with anything that is also using the static ground-body (colliders without a Rigidbody2D). Either turn this option on or set the connected body to another static Rigidbody2D.
     
    Krijojo likes this.
  4. Krijojo

    Krijojo

    Joined:
    Sunday
    Posts:
    3
    Thanks a lot! It works perfectly with the option on!
     
    MelvMay likes this.