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:
_joint = gameObject.AddComponent<DistanceJoint2D>();
_friction = gameObject.AddComponent<FrictionJoint2D>();
Rigidbody2D otherRb = _selectedStar.GetComponent<Rigidbody2D>();
_joint.distance = (_position - otherRb.position).magnitude;
_joint.maxDistanceOnly = _joint.distance < _minHangDistance;
_joint.connectedBody = otherRb;
_joint.autoConfigureDistance = false;
_joint.enableCollision = true;
_friction.maxForce = 1;
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?