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
- Add
Rigidbody2Dto the moving body. - Add
DistanceJoint2D. - Assign
Connected Bodyif it should attach to another rigidbody. - Place
AnchorandConnected Anchor. - Decide whether
Auto Configure Distanceshould stay on. - 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 Distanceenabled
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 DistanceMax 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:
- Give the lantern a
Rigidbody2D. - Add
DistanceJoint2D. - Connect it to the ceiling body.
- Keep
Auto Configure Distanceon while you establish the connection. - 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;
}
}