From 1de94528c8d985f004bc97353cda8e81d2fc42a9 Mon Sep 17 00:00:00 2001 From: charnet3d Date: Sat, 2 Mar 2024 12:39:01 +0100 Subject: [PATCH] Section 13: The Enemy Class - Lecture 254 Explosive --- Config/DefaultEngine.ini | 4 ++ .../Assets/Sounds/Ambient/Collapse01.uasset | 3 ++ .../Assets/Sounds/Ambient/Collapse02.uasset | 3 ++ .../Assets/Sounds/Ambient/Explosion.uasset | 3 ++ .../Assets/Sounds/Ambient/Explosion01.uasset | 3 ++ .../Assets/Sounds/Ambient/Explosion02.uasset | 3 ++ .../_Game/Assets/Sounds/Ambient/Fire01.uasset | 3 ++ .../Sounds/Ambient/Fire_Sparks01.uasset | 3 ++ .../Assets/Sounds/Ambient/Light01.uasset | 3 ++ .../Assets/Sounds/Ambient/Light02.uasset | 3 ++ .../Assets/Sounds/Ambient/Smoke01.uasset | 3 ++ .../Sounds/Ambient/Starter_Birds01.uasset | 3 ++ .../Sounds/Ambient/Starter_Music01.uasset | 3 ++ .../Sounds/Ambient/Starter_Wind05.uasset | 3 ++ .../Sounds/Ambient/Starter_Wind06.uasset | 3 ++ .../Assets/Sounds/Ambient/Steam01.uasset | 3 ++ .../Assets/Sounds/Impacts/FleshHit_Cue.uasset | 3 ++ Content/_Game/Enemies/EnemyBP.uasset | 3 ++ Content/_Game/Explosives/ExplosiveBP.uasset | 3 ++ Content/_Game/Maps/DefaultMap.umap | 4 +- Source/Shooter/BulletHitInterface.cpp | 6 +++ Source/Shooter/BulletHitInterface.h | 28 ++++++++++ Source/Shooter/Enemy.cpp | 51 +++++++++++++++++++ Source/Shooter/Enemy.h | 41 +++++++++++++++ Source/Shooter/Explosive.cpp | 47 +++++++++++++++++ Source/Shooter/Explosive.h | 41 +++++++++++++++ Source/Shooter/ShooterCharacter.cpp | 44 ++++++++++------ Source/Shooter/ShooterCharacter.h | 2 +- 28 files changed, 304 insertions(+), 18 deletions(-) create mode 100644 Content/_Game/Assets/Sounds/Ambient/Collapse01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Collapse02.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Explosion.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Explosion01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Explosion02.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Fire01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Fire_Sparks01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Light01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Light02.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Smoke01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Starter_Birds01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Starter_Music01.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Starter_Wind05.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Starter_Wind06.uasset create mode 100644 Content/_Game/Assets/Sounds/Ambient/Steam01.uasset create mode 100644 Content/_Game/Assets/Sounds/Impacts/FleshHit_Cue.uasset create mode 100644 Content/_Game/Enemies/EnemyBP.uasset create mode 100644 Content/_Game/Explosives/ExplosiveBP.uasset create mode 100644 Source/Shooter/BulletHitInterface.cpp create mode 100644 Source/Shooter/BulletHitInterface.h create mode 100644 Source/Shooter/Enemy.cpp create mode 100644 Source/Shooter/Enemy.h create mode 100644 Source/Shooter/Explosive.cpp create mode 100644 Source/Shooter/Explosive.h diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index 062440a9..9fd02a73 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -47,6 +47,10 @@ r.DynamicGlobalIlluminationMethod=1 r.ReflectionMethod=1 r.Shadow.Virtual.Enable=1 r.CustomDepth=3 +r.Streaming.LimitPoolSizeToVRAM=1 +r.Streaming.PoolSize=0 +r.MSAACount=2 +r.AntiAliasingMethod=2 [/Script/WorldPartitionEditor.WorldPartitionEditorSettings] CommandletClass=Class'/Script/UnrealEd.WorldPartitionConvertCommandlet' diff --git a/Content/_Game/Assets/Sounds/Ambient/Collapse01.uasset b/Content/_Game/Assets/Sounds/Ambient/Collapse01.uasset new file mode 100644 index 00000000..74958b61 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Collapse01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8f69515436cd6e5e8724858ed2b11556302ae08af4abfd510f04d51a6376209f +size 320131 diff --git a/Content/_Game/Assets/Sounds/Ambient/Collapse02.uasset b/Content/_Game/Assets/Sounds/Ambient/Collapse02.uasset new file mode 100644 index 00000000..1c2b6cdf --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Collapse02.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a473a93233ff0296a5f37b8852d365f7e7e1fab85960033b4f00361122590a5 +size 324444 diff --git a/Content/_Game/Assets/Sounds/Ambient/Explosion.uasset b/Content/_Game/Assets/Sounds/Ambient/Explosion.uasset new file mode 100644 index 00000000..ab50389a --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Explosion.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b1203498b8f932995d3d3575c8d853db03f2abf552fa6a65fba44fc91124914 +size 6448 diff --git a/Content/_Game/Assets/Sounds/Ambient/Explosion01.uasset b/Content/_Game/Assets/Sounds/Ambient/Explosion01.uasset new file mode 100644 index 00000000..909ca169 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Explosion01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:079c202e5b18baf764c69b917afc29f8de554f286161fa0c8921cb2a8fdb73da +size 212530 diff --git a/Content/_Game/Assets/Sounds/Ambient/Explosion02.uasset b/Content/_Game/Assets/Sounds/Ambient/Explosion02.uasset new file mode 100644 index 00000000..81064f78 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Explosion02.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2eda941da70aca11673907b258645f2c0eb64f093ed33689f0b6478930a88635 +size 208748 diff --git a/Content/_Game/Assets/Sounds/Ambient/Fire01.uasset b/Content/_Game/Assets/Sounds/Ambient/Fire01.uasset new file mode 100644 index 00000000..344ef7f7 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Fire01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3c92452baa38fc690262653f253d1bbafddfff0248a35aa62d6bb69e023a6a39 +size 521232 diff --git a/Content/_Game/Assets/Sounds/Ambient/Fire_Sparks01.uasset b/Content/_Game/Assets/Sounds/Ambient/Fire_Sparks01.uasset new file mode 100644 index 00000000..a145a766 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Fire_Sparks01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0e396ae63aee5a17ce4dee83b1290cec38ebfff45799ee2dfb80b0eacd10421c +size 705161 diff --git a/Content/_Game/Assets/Sounds/Ambient/Light01.uasset b/Content/_Game/Assets/Sounds/Ambient/Light01.uasset new file mode 100644 index 00000000..928611ff --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Light01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bf612c048af41bc174d2de398801ee004783cb5d2fa1e65b9e45f1c4ee55f358 +size 319258 diff --git a/Content/_Game/Assets/Sounds/Ambient/Light02.uasset b/Content/_Game/Assets/Sounds/Ambient/Light02.uasset new file mode 100644 index 00000000..3f4ee153 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Light02.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2bd7544352b88a8c00ee471462b24902c63ae76a2c414759c3d8e1247867138f +size 273132 diff --git a/Content/_Game/Assets/Sounds/Ambient/Smoke01.uasset b/Content/_Game/Assets/Sounds/Ambient/Smoke01.uasset new file mode 100644 index 00000000..137378aa --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Smoke01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:201ac363acbc914951cfa6d033eb76afb157bf49ba92e0c2826b9bde00d306b0 +size 444517 diff --git a/Content/_Game/Assets/Sounds/Ambient/Starter_Birds01.uasset b/Content/_Game/Assets/Sounds/Ambient/Starter_Birds01.uasset new file mode 100644 index 00000000..038134da --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Starter_Birds01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b77a743758f2ca6d6e0e854ab413ca62027615abe74841c302052e2b52328ab7 +size 2198388 diff --git a/Content/_Game/Assets/Sounds/Ambient/Starter_Music01.uasset b/Content/_Game/Assets/Sounds/Ambient/Starter_Music01.uasset new file mode 100644 index 00000000..61d4aff2 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Starter_Music01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2b8fa6eb7c973c56c7bf0e4207f3b0a64f7307dc45518cac6db97a29c75b8a11 +size 7416810 diff --git a/Content/_Game/Assets/Sounds/Ambient/Starter_Wind05.uasset b/Content/_Game/Assets/Sounds/Ambient/Starter_Wind05.uasset new file mode 100644 index 00000000..e4fb2a1e --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Starter_Wind05.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5e0116f1c83b85398f84f6d0acb7ed4ed936b63869843ce3bc34f70236a74a6 +size 1971050 diff --git a/Content/_Game/Assets/Sounds/Ambient/Starter_Wind06.uasset b/Content/_Game/Assets/Sounds/Ambient/Starter_Wind06.uasset new file mode 100644 index 00000000..672774b2 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Starter_Wind06.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3a0692d627fd5556f63471d4212c3a1a707180323b4fef3fbd72823b87abd2ac +size 1908770 diff --git a/Content/_Game/Assets/Sounds/Ambient/Steam01.uasset b/Content/_Game/Assets/Sounds/Ambient/Steam01.uasset new file mode 100644 index 00000000..39b531ae --- /dev/null +++ b/Content/_Game/Assets/Sounds/Ambient/Steam01.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:42d449beef2c63649f130cd2245c3f09f349a631d298236ac9cb00ca71f6d278 +size 724043 diff --git a/Content/_Game/Assets/Sounds/Impacts/FleshHit_Cue.uasset b/Content/_Game/Assets/Sounds/Impacts/FleshHit_Cue.uasset new file mode 100644 index 00000000..c7f994b5 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Impacts/FleshHit_Cue.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fdfaddcf71469ac51027d4e411792d4884d929d065902d20782dfce5bc2266d1 +size 16031 diff --git a/Content/_Game/Enemies/EnemyBP.uasset b/Content/_Game/Enemies/EnemyBP.uasset new file mode 100644 index 00000000..f188c9f0 --- /dev/null +++ b/Content/_Game/Enemies/EnemyBP.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fa4907ef2bd55e2cbb48635c541670a25db03db5022a68431a69296e083e773c +size 33675 diff --git a/Content/_Game/Explosives/ExplosiveBP.uasset b/Content/_Game/Explosives/ExplosiveBP.uasset new file mode 100644 index 00000000..5d942792 --- /dev/null +++ b/Content/_Game/Explosives/ExplosiveBP.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01827014006f7491557e32963969cd7e9cffb1b815fee08fccbbe9325e664d60 +size 29076 diff --git a/Content/_Game/Maps/DefaultMap.umap b/Content/_Game/Maps/DefaultMap.umap index 1b310eb5..c2bd1d8f 100644 --- a/Content/_Game/Maps/DefaultMap.umap +++ b/Content/_Game/Maps/DefaultMap.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2f22d8d7bef43fd854a3776a795871e0776c1874226385c272a06de2693747f1 -size 206901 +oid sha256:c1e0103f628f1bbf30af806645009416132181302b04cbf92c932650e9471b1c +size 237612 diff --git a/Source/Shooter/BulletHitInterface.cpp b/Source/Shooter/BulletHitInterface.cpp new file mode 100644 index 00000000..da47c315 --- /dev/null +++ b/Source/Shooter/BulletHitInterface.cpp @@ -0,0 +1,6 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "BulletHitInterface.h" + +// Add default functionality here for any IBulletHitInterface functions that are not pure virtual. diff --git a/Source/Shooter/BulletHitInterface.h b/Source/Shooter/BulletHitInterface.h new file mode 100644 index 00000000..f7c52503 --- /dev/null +++ b/Source/Shooter/BulletHitInterface.h @@ -0,0 +1,28 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "UObject/Interface.h" +#include "BulletHitInterface.generated.h" + +// This class does not need to be modified. +UINTERFACE(MinimalAPI) +class UBulletHitInterface : public UInterface +{ + GENERATED_BODY() +}; + +/** + * + */ +class SHOOTER_API IBulletHitInterface +{ + GENERATED_BODY() + + // Add interface functions to this class. This is the class that will be inherited to implement this interface. +public: + + UFUNCTION(BlueprintNativeEvent, BlueprintCallable) + void BulletHit(FHitResult HitResult); +}; diff --git a/Source/Shooter/Enemy.cpp b/Source/Shooter/Enemy.cpp new file mode 100644 index 00000000..bff121b6 --- /dev/null +++ b/Source/Shooter/Enemy.cpp @@ -0,0 +1,51 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "Enemy.h" +#include "Kismet/GameplayStatics.h" +#include "Sound/SoundCue.h" +#include "Particles/ParticleSystemComponent.h" + +// Sets default values +AEnemy::AEnemy() +{ + // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; + +} + +// Called when the game starts or when spawned +void AEnemy::BeginPlay() +{ + Super::BeginPlay(); + + GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block); +} + +// Called every frame +void AEnemy::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + +// Called to bind functionality to input +void AEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + +} + +void AEnemy::BulletHit_Implementation(FHitResult HitResult) +{ + if (ImpactSound) + { + UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation()); + } + + if (ImpactParticles) + { + UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, HitResult.Location, FRotator(0.f), true); + } +} + diff --git a/Source/Shooter/Enemy.h b/Source/Shooter/Enemy.h new file mode 100644 index 00000000..cac25f21 --- /dev/null +++ b/Source/Shooter/Enemy.h @@ -0,0 +1,41 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BulletHitInterface.h" +#include "GameFramework/Character.h" +#include "Enemy.generated.h" + +class UParticleSystem; +class USoundCue; + +UCLASS() +class SHOOTER_API AEnemy : public ACharacter, public IBulletHitInterface +{ + GENERATED_BODY() + +public: + // Sets default values for this character's properties + AEnemy(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + /** Particles to spawn when hit by bullets */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) + UParticleSystem* ImpactParticles; + + /** Sound to play when hit by bullets */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) + USoundCue* ImpactSound; +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + + // Called to bind functionality to input + virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + + virtual void BulletHit_Implementation(FHitResult HitResult) override; +}; diff --git a/Source/Shooter/Explosive.cpp b/Source/Shooter/Explosive.cpp new file mode 100644 index 00000000..d9c2603e --- /dev/null +++ b/Source/Shooter/Explosive.cpp @@ -0,0 +1,47 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "Explosive.h" +#include "Kismet/GameplayStatics.h" +#include "Sound/SoundCue.h" +#include "Particles/ParticleSystemComponent.h" + +// Sets default values +AExplosive::AExplosive() +{ + // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; + +} + +// Called when the game starts or when spawned +void AExplosive::BeginPlay() +{ + Super::BeginPlay(); + +} + +// Called every frame +void AExplosive::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + +void AExplosive::BulletHit_Implementation(FHitResult HitResult) +{ + if (ExplosionSound) + { + UGameplayStatics::PlaySoundAtLocation(this, ExplosionSound, GetActorLocation()); + } + + if (ExplosionParticles) + { + UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticles, HitResult.Location, FRotator(0.f), true); + } + + // TODO: Apply explosive damage + + Destroy(); +} + diff --git a/Source/Shooter/Explosive.h b/Source/Shooter/Explosive.h new file mode 100644 index 00000000..b9356fb4 --- /dev/null +++ b/Source/Shooter/Explosive.h @@ -0,0 +1,41 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BulletHitInterface.h" +#include "GameFramework/Actor.h" +#include "Explosive.generated.h" + +class UParticleSystem; +class USoundCue; + +UCLASS() +class SHOOTER_API AExplosive : public AActor, public IBulletHitInterface +{ + GENERATED_BODY() + +public: + // Sets default values for this actor's properties + AExplosive(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + +private: + + /** Particles to spawn when hit by bullets */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) + UParticleSystem* ExplosionParticles; + + /** Sound to play when hit by bullets */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) + USoundCue* ExplosionSound; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + + virtual void BulletHit_Implementation(FHitResult HitResult) override; +}; diff --git a/Source/Shooter/ShooterCharacter.cpp b/Source/Shooter/ShooterCharacter.cpp index a58eb443..6b4c1961 100644 --- a/Source/Shooter/ShooterCharacter.cpp +++ b/Source/Shooter/ShooterCharacter.cpp @@ -17,6 +17,7 @@ #include "Particles/ParticleSystemComponent.h" #include "Sound/SoundCue.h" #include "Ammo.h" +#include "BulletHitInterface.h" #include "PhysicalMaterials/PhysicalMaterial.h" #include "Shooter.h" @@ -654,16 +655,29 @@ void AShooterCharacter::SendBullet() UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), EquippedWeapon->GetMuzzleFlash(), SocketTransform); } - FVector BeamEnd; - bool bBeamEnd = GetBeamEndLocation(SocketTransform.GetLocation(), BeamEnd); - + FHitResult BeamHitResult; + bool bBeamEnd = GetBeamEndLocation(SocketTransform.GetLocation(), BeamHitResult); if (!bBeamEnd) return; // Spawn impact particles after updating BeamEndPoint - if (ImpactParticles) + + // Does hit actor implement BulletHitInterface? + IBulletHitInterface* BulletHitInterface = nullptr; + if (IsValid(BeamHitResult.GetActor()) && + (BulletHitInterface = Cast(BeamHitResult.GetActor())) != nullptr) { - UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, - BeamEnd); + BulletHitInterface->BulletHit_Implementation(BeamHitResult); + UE_LOG(LogTemp, Warning, TEXT("actor particle")); + GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("actor particle")); + } + else + { + if (ImpactParticles) + { + UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, + BeamHitResult.Location); + UE_LOG(LogTemp, Warning, TEXT("normal particle")); + } } // Spawn smoke trail particles @@ -673,10 +687,9 @@ void AShooterCharacter::SendBullet() SocketTransform); if (Beam) { - Beam->SetVectorParameter("Target", BeamEnd); + Beam->SetVectorParameter("Target", BeamHitResult.Location); } } - } void AShooterCharacter::PlayGunFireMontage() @@ -691,27 +704,28 @@ void AShooterCharacter::PlayGunFireMontage() } } -bool AShooterCharacter::GetBeamEndLocation(const FVector& MuzzleSocketLocation, FVector& OutBeamLocation) +bool AShooterCharacter::GetBeamEndLocation(const FVector& MuzzleSocketLocation, FHitResult& OutHitResult) { + FVector OutBeamLocation; + // Check for crosshair trace hit FHitResult CrosshairHitResult; bool bCrosshairHit = TraceUnderCrosshairs(CrosshairHitResult, OutBeamLocation); // Perform a second trace, this time from the gun barrel - FHitResult WeaponTraceHit; const FVector WeaponTraceStart{ MuzzleSocketLocation }; const FVector StartToEnd{ OutBeamLocation - MuzzleSocketLocation }; const FVector WeaponTraceEnd{ MuzzleSocketLocation + StartToEnd * 1.25f }; - GetWorld()->LineTraceSingleByChannel(WeaponTraceHit, WeaponTraceStart, WeaponTraceEnd, + GetWorld()->LineTraceSingleByChannel(OutHitResult, WeaponTraceStart, WeaponTraceEnd, ECollisionChannel::ECC_Visibility); - if (WeaponTraceHit.bBlockingHit) // Object between barrel and BeamEndPoint ? + if (!OutHitResult.bBlockingHit) // Object between barrel and BeamEndPoint ? { - OutBeamLocation = WeaponTraceHit.Location; - return true; + OutHitResult.Location = OutBeamLocation; + return false; } - return false; + return true; } bool AShooterCharacter::TraceUnderCrosshairs(FHitResult& OutHitResult, FVector& OutHitLocation) diff --git a/Source/Shooter/ShooterCharacter.h b/Source/Shooter/ShooterCharacter.h index 7a45a3bf..76c533ce 100644 --- a/Source/Shooter/ShooterCharacter.h +++ b/Source/Shooter/ShooterCharacter.h @@ -104,7 +104,7 @@ protected: */ void FireWeapon(); - bool GetBeamEndLocation(const FVector& MuzzleSocketLocation, FVector& OutBeamLocation); + bool GetBeamEndLocation(const FVector& MuzzleSocketLocation, FHitResult& OutHitResult); /* Set bAiming to true or false with button press */ void AimingButtonPressed();