Guides Physics 2D Joints Use DistanceJoint2D

Physics 2D Joints 2 min read Updated Apr 2026

Use DistanceJoint2D

`DistanceJoint2D` keeps two bodies at a distance relationship.

Use it when you want two rigidbodies to behave like they are tethered, linked, or spring-constrained.

Good Fits

DistanceJoint2D is useful for:

  • rope-like links
  • hanging lanterns
  • tethered pickups
  • spring-style mechanical setups

Scene Setup

  1. Add Rigidbody2D to the moving body.
  2. Add DistanceJoint2D.
  3. Assign Connected Body if it should attach to another rigidbody.
  4. Place Anchor and Connected Anchor.
  5. Decide whether Auto Configure Distance should stay on.
  6. Use distance limits when the connection needs some slack range.

Start Simple

The easiest first version is:

  • one moving body
  • one connected body
  • Auto Configure Distance enabled

That gives you a usable tether without asking you to hand-author the first distance value.

Use Limits When a Fixed Distance Is Too Stiff

Turn on Use Limits when the joint should allow a range instead of a single distance target.

Then tune:

  • Min Distance
  • Max Distance

This is a better fit for rope-like motion than a perfectly rigid connection.

Practical Example: Hanging Lantern

Use DistanceJoint2D for a lantern hanging from a ceiling anchor:

  1. Give the lantern a Rigidbody2D.
  2. Add DistanceJoint2D.
  3. Connect it to the ceiling body.
  4. Keep Auto Configure Distance on while you establish the connection.
  5. Enable limits if you want a little travel range.

That gives you a readable hanging connection without needing a custom spring script first.

PHP Example

<?php

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\DistanceJoint2D;

class HangingLantern extends Behaviour
{
    public function awake(): void
    {
        $joint = $this->gameObject->getComponent(DistanceJoint2D::class);
        if ($joint === null) {
            return;
        }

        $joint->autoConfigureDistance = false;
        $joint->distance = 3.0;
        $joint->enableLimit = true;
        $joint->minDistance = 2.8;
        $joint->maxDistance = 3.4;
    }
}

Related Guides