terminalCode Example
Physics joints — Fixed, Ball, Hinge, Slider, Spring, and Wheel
Physics Joints
Joints connect two physics bodies together with constraints. All joint types are components added to a GameObject positioned at the joint's anchor point.
Joint Types
| Type | Description |
|---|---|
| FixedJoint | Locks two bodies together rigidly |
| BallJoint | Allows rotation in all axes (ball-and-socket) |
| HingeJoint | Rotation around one axis (doors, wheels) |
| SliderJoint | Linear movement along one axis (pistons) |
| SpringJoint | Elastic connection with configurable stiffness |
| WheelJoint | Combined hinge + suspension for vehicles |
Basic Setup
CSHARP
// All joints need two bodies: the joint's own GO body and an anchor body.
// Position the joint GO at the connection point.
var joint = gameObject.Components.Create<HingeJoint>();
joint.Body = doorGameObject; // the body being constrained
joint.AnchorBody = frameGameObject; // what it's attached to (null = world)
joint.Strength = 0f; // 0 = rigid, >0 = breakableHinge Joint (Door)
CSHARP
public sealed class Door : Component
{
protected override void OnStart()
{
var hinge = Components.Create<HingeJoint>();
hinge.Body = Components.Get<Rigidbody>().GameObject;
hinge.AnchorBody = frameObject;
// Axis defaults to local Z — set GO rotation to align the hinge axis
}
}Spring Joint
CSHARP
var spring = gameObject.Components.Create<SpringJoint>();
spring.Body = objectA;
spring.AnchorBody = objectB;
spring.Strength = 500f; // spring stiffnessWheel Joint (Vehicle)
CSHARP
var wheel = axleObject.Components.Create<WheelJoint>();
wheel.Body = wheelObject;
wheel.AnchorBody = chassisObject;
wheel.Strength = 1000f; // suspension strengthBreaking Joints
Set Strength to a positive value — the joint breaks when force exceeds it:
CSHARP
joint.Strength = 2000f; // breaks at 2000 force units
joint.AngularStrength = 500f; // separate limit for torqueChecking if a Joint is Broken
CSHARP
public sealed class BreakableJoint : Component
{
HingeJoint Joint { get; set; }
protected override void OnUpdate()
{
if ( Joint == null || !Joint.IsValid() )
{
Log.Info( "Joint broke!" );
}
}
}
Was this helpful?