Update AI Characters for Ragdoll on Death

This commit is contained in:
Philip W 2023-10-07 01:05:17 +01:00
parent d4b7c4b2d6
commit 8a8421c03f
4 changed files with 56 additions and 8 deletions

View File

@ -4,6 +4,8 @@
#include "CompanionCharacter.h"
#include "AI_CompanionController.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Perception/AIPerceptionStimuliSourceComponent.h"
#include "Perception/AISense_Sight.h"
@ -19,7 +21,6 @@ ACompanionCharacter::ACompanionCharacter()
void ACompanionCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
@ -39,7 +40,7 @@ UBehaviorTree* ACompanionCharacter::GetBehaviorTree() const
return BehaviorTree;
}
void ACompanionCharacter::TakeDamage(float DamageAmount)
void ACompanionCharacter::TakeDamage(const float DamageAmount)
{
CurrentHealth -= DamageAmount;
if (CurrentHealth <= 0)
@ -49,6 +50,29 @@ void ACompanionCharacter::TakeDamage(float DamageAmount)
const AAI_CompanionController* AIController = Cast<AAI_CompanionController>(GetController());
AIController->GetBrainComponent()->StopLogic("Companion is dead");
//Ragdoll
DetachFromControllerPendingDestroy();
UCapsuleComponent* CapsuleComp = GetCapsuleComponent();
CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore);
GetMesh()->SetCollisionProfileName(TEXT("Ragdoll"));
SetActorEnableCollision(true);
GetMesh()->SetAllBodiesSimulatePhysics(true);
GetMesh()->SetSimulatePhysics(true);
GetMesh()->WakeAllRigidBodies();
GetMesh()->bBlendPhysics = true;
if (UCharacterMovementComponent* CharacterComp = Cast<UCharacterMovementComponent>(GetMovementComponent()))
{
CharacterComp->StopMovementImmediately();
CharacterComp->DisableMovement();
CharacterComp->SetComponentTickEnabled(false);
}
SetLifeSpan(30.0f);
}
}
@ -61,4 +85,3 @@ void ACompanionCharacter::SetupStimuliSourceComponent()
StimuliSourceComponent->RegisterWithPerceptionSystem();
}
}

View File

@ -4,6 +4,8 @@
#include "EnemyCharacter.h"
#include "AI_EnemyController.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Perception/AIPerceptionStimuliSourceComponent.h"
#include "Perception/AISense_Sight.h"
@ -21,7 +23,6 @@ AEnemyCharacter::AEnemyCharacter()
void AEnemyCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
@ -41,7 +42,7 @@ UBehaviorTree* AEnemyCharacter::GetBehaviorTree() const
return BehaviorTree;
}
void AEnemyCharacter::TakeDamage(float DamageAmount)
void AEnemyCharacter::TakeDamage(const float DamageAmount)
{
CurrentHealth -= DamageAmount;
if (CurrentHealth <= 0)
@ -51,6 +52,29 @@ void AEnemyCharacter::TakeDamage(float DamageAmount)
const AAI_EnemyController* AIController = Cast<AAI_EnemyController>(GetController());
AIController->GetBrainComponent()->StopLogic("Enemy is dead");
//Ragdoll
DetachFromControllerPendingDestroy();
UCapsuleComponent* CapsuleComp = GetCapsuleComponent();
CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore);
GetMesh()->SetCollisionProfileName(TEXT("Ragdoll"));
SetActorEnableCollision(true);
GetMesh()->SetAllBodiesSimulatePhysics(true);
GetMesh()->SetSimulatePhysics(true);
GetMesh()->WakeAllRigidBodies();
GetMesh()->bBlendPhysics = true;
if (UCharacterMovementComponent* CharacterComp = Cast<UCharacterMovementComponent>(GetMovementComponent()))
{
CharacterComp->StopMovementImmediately();
CharacterComp->DisableMovement();
CharacterComp->SetComponentTickEnabled(false);
}
SetLifeSpan(30.0f);
}
}
@ -63,4 +87,3 @@ void AEnemyCharacter::SetupStimuliSourceComponent()
StimuliSourceComponent->RegisterWithPerceptionSystem();
}
}

View File

@ -6,7 +6,9 @@
#include "Components/CapsuleComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "AI/EnemyCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/GameplayStatics.h"
//////////////////////////////////////////////////////////////////////////
@ -107,7 +109,7 @@ void AEndlessVendettaCharacter::SetUnCrouch()
UnCrouch();
}
void AEndlessVendettaCharacter::TakeDamage(float DamageAmount)
void AEndlessVendettaCharacter::TakeDamage(const float DamageAmount)
{
CurrentHealth -= DamageAmount;
if (CurrentHealth <= 0)

View File

@ -76,7 +76,7 @@ public:
float DefaultHealth = 100.0f;
protected:
virtual void BeginPlay();
virtual void BeginPlay() override;
UPROPERTY(EditDefaultsOnly, Category = "Gadget")
TSubclassOf<AGadgetManager> GadgetManagerClass;