Hinge joints are used to simulate the connection of two objects through an axis, allowing them to rotate freely around this axis. This type of joint is commonly used to implement door hinges, wheels, pendulums, and other physical effects that require rotation around a fixed axis.
When adding a joint component, make sure the target entity already has a Dynamic Collider Component
attached. If not, the editor will automatically add a Dynamic Collider Component
for you.
If the target is a collider, the target entity needs to have a collider component (Dynamic Collider, Static Collider, Character Controller) attached.
anchor
Defines the anchor point on the main collider (local coordinates). This point is the rotation center of the hinge.
connectedAnchor
Defines the connection point position:
automaticConnectedAnchor
Whether to automatically calculate the connection point position. When enabled, it automatically maintains the initial relative position of the objects.
useLimits
Whether to enable angle limits. When enabled, the rotation range of the hinge can be limited.
limits
Sets the rotation range of the hinge:
Property | Description |
---|---|
min | Minimum angle limit (degrees) |
max | Maximum angle limit (degrees) |
contactDistance | Contact distance, defines the distance at which the limit starts to take effect |
stiffness | Spring stiffness (only effective when useSpring is true) |
damping | Damping coefficient (only effective when useSpring is true) |
useMotor
Whether to enable the motor function. When enabled, the hinge can be actively driven to rotate.
motor
Motor parameter settings:
Property | Description |
---|---|
targetVelocity | Target angular velocity (degrees/second) |
forceLimit | Maximum torque limit |
freeSpin | Whether to allow free spin |
gearRatio | Gear ratio, used to adjust the actual output angular velocity |
breakForce
The maximum force the joint can withstand before breaking.
breakTorque
The maximum torque the joint can withstand before breaking.
// Add Hinge Joint component
const hingeJoint = entity.addComponent(HingeJoint);
// Set the connected object
hingeJoint.connectedCollider = targetEntity.getComponent(Collider);
// Set the rotation axis
hingeJoint.axis.setValue(0, 1, 0); // Rotate around Y-axis
// Enable angle limits
hingeJoint.useLimits = true;
hingeJoint.limits = new JointLimits();
hingeJoint.limits.min = -45; // Minimum angle
hingeJoint.limits.max = 45; // Maximum angle
hingeJoint.limits.contactDistance = 5; // Contact distance
// Enable elastic limits
hingeJoint.useSpring = true;
hingeJoint.limits.stiffness = 100; // Spring stiffness
hingeJoint.limits.damping = 0.2; // Damping coefficient
// Enable motor
hingeJoint.useMotor = true;
hingeJoint.motor = new JointMotor();
hingeJoint.motor.targetVelocity = 180; // Target angular velocity (degrees/second)
hingeJoint.motor.forceLimit = 500; // Maximum torque
hingeJoint.motor.freeSpin = false; // Free spin
hingeJoint.motor.gearRatio = 1; // Gear ratio
// Get current angle
const currentAngle = hingeJoint.angle;
// Get current angular velocity
const angularVelocity = hingeJoint.velocity;