Spring joints are used to maintain distance constraints between two objects by controlling their relative motion through spring force and damping. They can set minimum and maximum distance ranges and apply elastic constraint forces when objects exceed these ranges.
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 using local coordinates. This point defines the connection position of the joint.
connectedAnchor
Defines the connection point position. Its meaning depends on the connectedCollider setting:
automaticConnectedAnchor
Whether to automatically calculate the value of connectedAnchor. When enabled, the system automatically sets the connection point to ensure the initial position relationship between objects. If you need precise manual control of the connection point, set this property to false.
minDistance
The minimum allowed distance. When the distance between objects is less than this value, the joint applies a pushing force.
maxDistance
The maximum allowed distance. When the distance between objects exceeds this value, the joint applies a pulling force.
tolerance
The tolerance range value, default is 0.25. This value determines when the joint starts to apply constraint forces:
stiffness
The spring stiffness coefficient. A higher value produces a stronger restoring force, making the spring behave "stiffer".
damping
The damping coefficient. Used to suppress spring oscillations, a higher value makes the motion stop faster.
breakForce
The maximum force the joint can withstand before breaking. Setting it to Infinity means the joint will never break due to force. This property can be used to simulate destructible connections between objects.
breakTorque
The maximum torque the joint can withstand before breaking. Setting it to Infinity means the joint will never break due to torque. Used in conjunction with breakForce to more realistically simulate the destruction of connections.
// Add Spring Joint component
const springJoint = entity.addComponent(SpringJoint);
// Set the connected object
springJoint.connectedCollider = targetEntity.getComponent(Collider);
// Set distance range
springJoint.minDistance = 1;
springJoint.maxDistance = 5;
// Configure spring properties
springJoint.stiffness = 50; // Spring stiffness
springJoint.damping = 0.2; // Damping coefficient
// Set tolerance
springJoint.tolerance = 0.25;