Use it when you want separate rigidbodies to behave like one connected piece.
Good Fits
FixedJoint2D is useful for:
- welded props
- temporary rigid attachments
- composite moving rigs
- physics objects that should stay locked together
Scene Setup
- Add
Rigidbody2Dto the body you want to lock. - Add
FixedJoint2D. - Assign
Connected Body. - Place
AnchorandConnected Anchor. - Leave
Enable Collisionoff unless you deliberately want the connected bodies to keep colliding with each other.
Practical Example: A Two-Part Crate Rig
Imagine a crate with a decorative top piece that still needs to move as part of the same rigid assembly.
- Add
Rigidbody2Dto the moving piece. - Add
FixedJoint2D. - Connect it to the main crate body.
- Align the anchors where the pieces should lock together.
Now the rig stays physically connected without needing to merge everything into one sprite or one collider setup.
PHP Example
<?php
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\FixedJoint2D;
use Lenga\Engine\Core\Vector3;
class LockRigTogether extends Behaviour
{
public function awake(): void
{
$joint = $this->gameObject->getComponent(FixedJoint2D::class);
if ($joint === null) {
return;
}
$joint->autoConfigureConnectedAnchor = false;
$joint->anchor = new Vector3(0.0, 0.5, 0.0);
$joint->connectedAnchor = new Vector3(0.0, -0.5, 0.0);
$joint->enableCollision = false;
}
}