Unreal is compiling the code but the enemy don’t spawn on the map. I made a blueprint for EnemyController and a blueprint for FPSShooterGameMode, i used the BP_Enemy for spawning enemies in BP_FPSShooterGameMode, I also selected for GameMode Override in World Settings the BP_FPSShooterGameMode but nothing happens, here is my code:
EnemyController.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Components/BoxComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "EnemyController.generated.h"
UCLASS()
class FPSSHOOTER_API AEnemyController : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AEnemyController();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
UPROPERTY(EditAnywhere)
UShapeComponent* RootBox;
UPROPERTY(EditAnywhere)
float Speed = 200.0f;
FVector Direction;
UFUNCTION()
void OnOverlap(UPrimitiveComponent* OverlappedCommponent, AActor* OtherActor,
UPrimitiveComponent* OtherComponent, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
};
EnemyController.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyController.h"
#include "Kismet/GameplayStatics.h"
#include "FPSShooterGameMode.h"
// Sets default values
AEnemyController::AEnemyController()
{
// 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;
RootBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
RootBox->bGenerateOverlapEvents = true;
RootBox->OnComponentBeginOverlap.AddDynamic(this, &AEnemyController::OnOverlap);
RootComponent = RootBox;
}
// Called when the game starts or when spawned
void AEnemyController::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AEnemyController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AEnemyController::OnOverlap(UPrimitiveComponent* OverlappedCommponent, AActor* OtherActor,
UPrimitiveComponent* OtherComponent, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (OtherActor == GetWorld()->GetFirstPlayerController()->GetPawn())
{
UGameplayStatics::SetGamePaused(GetWorld(), true);
}
}
FPSShooterGameMode.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "FPSShooterGameMode.generated.h"
/**
*
*/
UCLASS()
class FPSSHOOTER_API AFPSShooterGameMode : public AGameModeBase
{
GENERATED_BODY()
float MINIMUM_INTERVAL = 0.5f;
float MAXIMUM_INTERVAL = 2.0f;
float TIME_TO_MINIMUM_INTERVAL = 30.0f;
public:
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
void IncrementScore();
void OnGameOver();
void OnRestart();
UPROPERTY(EditAnywhere, Category = "Spawning")
TSubclassOf<class AEnemyController> EnemyBlueprint;
float EnemyTimer;
float GameTimer;
protected:
int Score = 0;
};
FPSShooterGameMode.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSShooterGameMode.h"
#include "EnemyController.h"
#include "FPSShooter.h"
void AFPSShooterGameMode::BeginPlay()
{
Super::BeginPlay();
}
void AFPSShooterGameMode::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
GameTimer += DeltaTime;
EnemyTimer -= DeltaTime;
if (EnemyTimer <= 0.0f)
{
float difficultyPercentage = FMath::Min(GameTimer / TIME_TO_MINIMUM_INTERVAL, 1.0f);
EnemyTimer = MAXIMUM_INTERVAL - (MAXIMUM_INTERVAL - MINIMUM_INTERVAL) * difficultyPercentage;
UWorld* World = GetWorld();
if (World)
{
float distance = 800.0f;
float randomAngle = FMath::RandRange(0.0f, 360.0f);
FVector playerLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
FVector enemyLocation = playerLocation;
enemyLocation.X += FMath::Cos(randomAngle * 3.14f / 180.0f) * distance;
enemyLocation.Y += FMath::Sin(randomAngle * 3.14f / 180.0f) * distance;
enemyLocation.Z = 210.0f;
AEnemyController* enemy = World->SpawnActor<AEnemyController>(
EnemyBlueprint, enemyLocation, FRotator::ZeroRotator);
}
}
}
void AFPSShooterGameMode::IncrementScore()
{
}
void AFPSShooterGameMode::OnGameOver()
{
}
void AFPSShooterGameMode::OnRestart()
{
}