terminalCode Example
ModelPhysics — ragdolls and per-bone physics simulation
ModelPhysics — Ragdolls and Per-Bone Physics
ModelPhysics drives physics simulation per bone on a skinned model. Use it for ragdolls, destructible characters, and physics-driven cloth or tails.Basic Ragdoll
CSHARP
public sealed class RagdollOnDeath : Component
{
[RequireComponent] SkinnedModelRenderer Model { get; set; }
public void Die()
{
// Disable the animator so physics takes over
var animator = Components.Get<AnimationController>();
if ( animator.IsValid() ) animator.Enabled = false;
// Add ModelPhysics — it reads the model's physics asset automatically
var ragdoll = Components.Create<ModelPhysics>();
ragdoll.MotionEnabled = true;
// Optionally inherit current velocity
if ( Components.TryGet<CharacterController>( out var cc ) )
{
foreach ( var body in ragdoll.PhysicsGroup.Bodies )
body.Velocity = cc.Velocity;
}
}
}Spawning a Ragdoll Prefab
CSHARP
public sealed class NpcDeath : Component
{
[Property] public GameObject RagdollPrefab { get; set; }
public void SpawnRagdoll( Vector3 deathVelocity )
{
if ( !RagdollPrefab.IsValid() ) return;
var ragdoll = RagdollPrefab.Clone( WorldPosition );
ragdoll.WorldRotation = WorldRotation;
// Copy clothing/skin from the living model
var srcRenderer = Components.Get<SkinnedModelRenderer>();
var dstRenderer = ragdoll.Components.Get<SkinnedModelRenderer>();
if ( srcRenderer.IsValid() && dstRenderer.IsValid() )
dstRenderer.CopyFrom( srcRenderer );
// Apply death velocity to all bones
var physics = ragdoll.Components.Get<ModelPhysics>();
if ( physics.IsValid() )
{
foreach ( var body in physics.PhysicsGroup.Bodies )
body.Velocity = deathVelocity;
}
// Destroy the original
GameObject.Destroy();
}
}Pinning Bones
Disable motion on specific bones to keep part of the model animated while the rest ragdolls:
CSHARP
var ragdoll = Components.Create<ModelPhysics>();
ragdoll.MotionEnabled = true;
// Pin the root bone so the body stays in place
var rootBody = ragdoll.PhysicsGroup.GetBody( "pelvis" );
if ( rootBody != null )
rootBody.MotionEnabled = false;Clothing Preservation on Ragdolls
When creating a ragdoll from an NPC with clothing, copy the SkinnedModelRenderer state before destroying the source:
CSHARP
// See NPC Ragdoll Creation with Clothing Preservation in the KB
// for the full pattern including Dresser component handling.
Was this helpful?