I was trying to complete the Ammo megachallenge, but the gun still keeps firing after the ammo is 0 and I am able to kill the AI. Here is my code -
Gun.cpp -
// Fill out your copyright notice in the Description page of Project Settings.
#include "Gun.h"
#include "Components/SkeletalMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"
// Sets default values
AGun::AGun()
{
// 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;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
void AGun::PullTrigger()
{
--Ammo;
if (Ammo != 0) {
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT("MuzzleFlashSocket"));
UGameplayStatics::SpawnSoundAttached(MuzzleSound, Mesh, TEXT("MuzzleFlashSocket"));
FHitResult Hit;
FVector ShotDirection;
bool bSuccess = GunTrace(Hit, ShotDirection);
if (bSuccess)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, Hit.Location, ShotDirection.Rotation());
UGameplayStatics::PlaySoundAtLocation(GetWorld(), ImpactSound, Hit.Location);
AActor* HitActor = Hit.GetActor();
if (HitActor != nullptr)
{
FPointDamageEvent DamageEvent(Damage, Hit, ShotDirection, nullptr);
AController* OwnerController = GetOwnerController();
HitActor->TakeDamage(Damage, DamageEvent, OwnerController, this);
}
}
}
else {
UE_LOG(LogTemp, Warning, TEXT("Ammo is empty!"));
UGameplayStatics::SpawnSoundAttached(MagEmptySound, Mesh, TEXT("MuzzleFlashSocket"));
}
}
// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool AGun::GunTrace(FHitResult& Hit, FVector& ShotDirection) {
AController* OwnerController = GetOwnerController();
if (OwnerController == nullptr)
return false;
FVector Location;
FRotator Rotation;
OwnerController->GetPlayerViewPoint(Location, Rotation);
ShotDirection = -Rotation.Vector();
FVector End = Location + Rotation.Vector() * MaxRange;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
Params.AddIgnoredActor(GetOwner());
return GetWorld()->LineTraceSingleByChannel(Hit, Location, End, ECollisionChannel::ECC_GameTraceChannel1, Params);
}
AController* AGun::GetOwnerController() const {
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if (OwnerPawn == nullptr)
return nullptr;
return OwnerPawn->GetController();
}
And Gun.h -
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"
UCLASS()
class SIMPLESHOOTER_API AGun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGun();
void PullTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnywhere)
USceneComponent* Root;
UPROPERTY(VisibleAnywhere)
USkeletalMeshComponent* Mesh;
UPROPERTY(EditAnywhere)
UParticleSystem* MuzzleFlash;
UPROPERTY(EditAnywhere)
USoundBase* MuzzleSound;
UPROPERTY(EditAnywhere)
USoundBase* ImpactSound;
UPROPERTY(EditAnywhere)
UParticleSystem* ImpactEffect;
UPROPERTY(EditAnywhere)
float MaxRange = 1000;
UPROPERTY(EditAnywhere)
float Damage = 10;
UPROPERTY(EditAnywhere)
float Ammo = 40.f;
UPROPERTY(EditAnywhere)
USoundBase* MagEmptySound;
bool GunTrace(FHitResult& Hit, FVector& ShotDirection);
AController* GetOwnerController() const;
};