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.
PrimaryActorTick.bCanEverTick = false;
StaticMeshComponent->SetCollisionProfileName(FName("PhysicsActor"));
StaticMeshComponent->SetCollisionProfileName(FName("BlockAll"));
StaticMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
StaticMeshComponent->SetCollisionObjectType(ECC_WorldDynamic);
StaticMeshComponent->SetSimulatePhysics(true);
StaticMeshComponent->SetGenerateOverlapEvents(true);
StaticMeshComponent->SetNotifyRigidBodyCollision(true);
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
@ -29,18 +26,19 @@ void AThrowable::BeginPlay()
{
Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AThrowable::OnOverlapBegin);
auto playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
StaticMeshComponent->OnComponentHit.AddDynamic(this, &AThrowable::OnOverlapBegin);
auto playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle;
StaticMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
}
void AThrowable::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
void AThrowable::OnOverlapBegin(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor)
if (!OtherActor->ActorHasTag(FName("Player")) && OtherActor != this)
{
if (HealthComponent)
{

View File

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

View File

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

View File

@ -9,28 +9,37 @@
AWeaponThrowable::AWeaponThrowable()
{
WeaponSkeletalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeletal Mesh Component"));
WeaponSkeletalMeshComponent->SetCollisionProfileName(FName("PhysicsActor"));
WeaponSkeletalMeshComponent->SetCollisionProfileName(FName("BlockAll"));
WeaponSkeletalMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponSkeletalMeshComponent->SetCollisionObjectType(ECC_WorldDynamic);
WeaponSkeletalMeshComponent->SetSimulatePhysics(true);
WeaponSkeletalMeshComponent->SetGenerateOverlapEvents(true);
WeaponSkeletalMeshComponent->SetNotifyRigidBodyCollision(true);
SetRootComponent(WeaponSkeletalMeshComponent);
}
void AWeaponThrowable::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();
playerForwardVector.Z = ImpulseAngle;
WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
}
void AWeaponThrowable::SetWeaponSkeletalMesh(USkeletalMesh* SkeletalMesh)
{
WeaponSkeletalMeshComponent->SetSkeletalMesh(SkeletalMesh);
auto playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle;
WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
if (auto playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))
{
auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle;
WeaponSkeletalMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
}
}

View File

@ -15,9 +15,9 @@ class NAKATOMI_API AWeaponThrowable : public AThrowable
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USkeletalMeshComponent* WeaponSkeletalMeshComponent;
public:
// Sets default values for this actor's properties
AWeaponThrowable();
@ -27,6 +27,5 @@ protected:
virtual void BeginPlay() override;
public:
void SetWeaponSkeletalMesh(USkeletalMesh* SkeletalMesh);
};