Skip to content

Instantly share code, notes, and snippets.

@sampattuzzi
Created April 3, 2020 10:47
Show Gist options
  • Save sampattuzzi/fe063f6c13c1376ed98c72a446ab64eb to your computer and use it in GitHub Desktop.
Save sampattuzzi/fe063f6c13c1376ed98c72a446ab64eb to your computer and use it in GitHub Desktop.
Enable ragdoll on a character with weapon
void AFPSCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (Health <= 0 && Controller != nullptr)
{
// Shouldn't be in control after we die.
DetachFromControllerPendingDestroy();
// Capsule collisions off
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GetCapsuleComponent()->SetCollisionResponseToAllChannels(ECR_Ignore);
// Mesh collisions on
GetMesh()->SetCollisionProfileName(FName(TEXT("Ragdoll")));
SetActorEnableCollision(true);
// Enable ragdoll physics
GetMesh()->SetAllBodiesSimulatePhysics(true);
GetMesh()->SetSimulatePhysics(true);
GetMesh()->WakeAllRigidBodies();
GetMesh()->bBlendPhysics = true;
// Give it a push
GetMesh()->AddImpulseToAllBodiesBelow(-GetActorForwardVector() * 500, TEXT("root"));
// Destroy after X seconds
SetLifeSpan(5);
// Weapon Ragdoll
if (RifleInstance && RifleInstance->Mesh)
{
RifleInstance->Mesh->SetCollisionProfileName(FName(TEXT("Ragdoll")));
RifleInstance->Mesh->SetAllBodiesSimulatePhysics(true);
RifleInstance->Mesh->SetSimulatePhysics(true);
RifleInstance->Mesh->WakeAllRigidBodies();
RifleInstance->Mesh->bBlendPhysics = true;
// Let it fly loose
RifleInstance->Mesh->DetachFromParent(true);
// Destroy after X seconds
RifleInstance->SetLifeSpan(5);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment