78 - Shell Physics

This commit is contained in:
Kingsmedia 2022-05-05 16:40:14 +02:00
parent 58df613558
commit 490af86ef1
30 changed files with 37 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,18 +3,42 @@
#include "Casing.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
ACasing::ACasing()
{
PrimaryActorTick.bCanEverTick = false;
ShellEjectionImpulseMin = 8.f;
ShellEjectionImpulseMax = 12.f;
CasingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CasingMesh"));
CasingMesh->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
CasingMesh->SetSimulatePhysics(true);
CasingMesh->SetEnableGravity(true);
CasingMesh->SetNotifyRigidBodyCollision(true);
SetRootComponent(CasingMesh);
}
void ACasing::BeginPlay()
{
Super::BeginPlay();
CasingMesh->OnComponentHit.AddDynamic(this, &ACasing::OnHit);
CasingMesh->AddImpulse(GetActorForwardVector() * FMath::RandRange(ShellEjectionImpulseMin, ShellEjectionImpulseMax));
SetLifeSpan(5.f);
}
void ACasing::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (ShellSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ShellSound, GetActorLocation());
}
// Disable hit event so that sound won't play multiple times
CasingMesh->SetNotifyRigidBodyCollision(false);
}

View File

@ -16,9 +16,21 @@ public:
protected:
virtual void BeginPlay() override;
UFUNCTION()
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* CasingMesh;
UPROPERTY(EditAnywhere)
float ShellEjectionImpulseMin;
UPROPERTY(EditAnywhere)
float ShellEjectionImpulseMax;
UPROPERTY(EditAnywhere)
class USoundCue* ShellSound;
};