Make Throwable Explode() on impact

This commit is contained in:
baz 2023-09-21 21:07:54 +01:00
parent d3e775758d
commit 6c0e0fe395
10 changed files with 46 additions and 42 deletions

Binary file not shown.

Binary file not shown.

BIN
Content/Player/PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

BIN
Content/Weapons/Machinegun/MachinegunThrowable.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -12,16 +12,13 @@ AThrowable::AThrowable()
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false; PrimaryActorTick.bCanEverTick = false;
StaticMeshComponent->SetCollisionProfileName(FName("PhysicsActor")); StaticMeshComponent->SetCollisionProfileName(FName("BlockAll"));
StaticMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); StaticMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
StaticMeshComponent->SetCollisionObjectType(ECC_WorldDynamic);
StaticMeshComponent->SetSimulatePhysics(true); StaticMeshComponent->SetSimulatePhysics(true);
StaticMeshComponent->SetGenerateOverlapEvents(true);
StaticMeshComponent->SetNotifyRigidBodyCollision(true);
SetRootComponent(StaticMeshComponent); SetRootComponent(StaticMeshComponent);
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
SphereComponent->SetSphereRadius(25.0f, true);
SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);
SphereComponent->SetCollisionProfileName(FName("IgnoreOnlyPawn"));
SphereComponent->SetupAttachment(RootComponent);
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned
@ -29,18 +26,19 @@ void AThrowable::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AThrowable::OnOverlapBegin); StaticMeshComponent->OnComponentHit.AddDynamic(this, &AThrowable::OnOverlapBegin);
auto playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
auto playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
auto playerForwardVector = playerCharacter->GetActorForwardVector(); auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle; playerForwardVector.Z = ImpulseAngle;
StaticMeshComponent->AddImpulse(playerForwardVector * ImpulseForce); StaticMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
} }
void AThrowable::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void AThrowable::OnOverlapBegin(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, FVector NormalImpulse, const FHitResult& Hit)
const FHitResult& SweepResult)
{ {
if (OtherActor) if (!OtherActor->ActorHasTag(FName("Player")) && OtherActor != this)
{ {
if (HealthComponent) if (HealthComponent)
{ {

View File

@ -12,7 +12,6 @@ class NAKATOMI_API AThrowable : public AExplosiveActor
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "0.0")) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "0.0"))
float ImpulseForce = 10000.f; float ImpulseForce = 10000.f;
@ -20,8 +19,7 @@ public:
float ImpulseAngle = 0.5f; float ImpulseAngle = 0.5f;
protected: protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UPROPERTY()
USphereComponent* SphereComponent; USphereComponent* SphereComponent;
public: public:
@ -33,12 +31,9 @@ protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
UFUNCTION() UFUNCTION()
virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, virtual void OnOverlapBegin(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
USphereComponent* GetSphereComponent() const { return SphereComponent; } USphereComponent* GetSphereComponent() const { return SphereComponent; }
}; };

View File

@ -49,7 +49,7 @@ protected:
TSubclassOf<class ANakatomiFieldSystemActor> FieldSystemActor; TSubclassOf<class ANakatomiFieldSystemActor> FieldSystemActor;
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
TSubclassOf<AWeaponThrowable> WeaponThrowableTemplate; TSubclassOf<class AWeaponThrowable> WeaponThrowableTemplate;
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's properties

View File

@ -9,9 +9,12 @@
AWeaponThrowable::AWeaponThrowable() AWeaponThrowable::AWeaponThrowable()
{ {
WeaponSkeletalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeletal Mesh Component")); WeaponSkeletalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeletal Mesh Component"));
WeaponSkeletalMeshComponent->SetCollisionProfileName(FName("PhysicsActor")); WeaponSkeletalMeshComponent->SetCollisionProfileName(FName("BlockAll"));
WeaponSkeletalMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); WeaponSkeletalMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponSkeletalMeshComponent->SetCollisionObjectType(ECC_WorldDynamic);
WeaponSkeletalMeshComponent->SetSimulatePhysics(true); WeaponSkeletalMeshComponent->SetSimulatePhysics(true);
WeaponSkeletalMeshComponent->SetGenerateOverlapEvents(true);
WeaponSkeletalMeshComponent->SetNotifyRigidBodyCollision(true);
SetRootComponent(WeaponSkeletalMeshComponent); SetRootComponent(WeaponSkeletalMeshComponent);
} }
@ -19,9 +22,12 @@ void AWeaponThrowable::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
auto playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); WeaponSkeletalMeshComponent->OnComponentHit.AddDynamic(this, &AWeaponThrowable::OnOverlapBegin);
auto playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
auto playerForwardVector = playerCharacter->GetActorForwardVector(); auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle; playerForwardVector.Z = ImpulseAngle;
WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce); WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
} }
@ -29,8 +35,11 @@ void AWeaponThrowable::SetWeaponSkeletalMesh(USkeletalMesh* SkeletalMesh)
{ {
WeaponSkeletalMeshComponent->SetSkeletalMesh(SkeletalMesh); WeaponSkeletalMeshComponent->SetSkeletalMesh(SkeletalMesh);
auto playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); if (auto playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))
{
auto playerForwardVector = playerCharacter->GetActorForwardVector(); auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle; playerForwardVector.Z = ImpulseAngle;
WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce); WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
} }
}

View File

@ -15,7 +15,7 @@ class NAKATOMI_API AWeaponThrowable : public AThrowable
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USkeletalMeshComponent* WeaponSkeletalMeshComponent; USkeletalMeshComponent* WeaponSkeletalMeshComponent;
public: public:
@ -27,6 +27,5 @@ protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
void SetWeaponSkeletalMesh(USkeletalMesh* SkeletalMesh); void SetWeaponSkeletalMesh(USkeletalMesh* SkeletalMesh);
}; };