Spawn enemy outside of camera view

This commit is contained in:
baz 2024-06-27 17:46:27 +01:00
parent 2e59bb6e0e
commit 24d5eb9eaf
1 changed files with 8 additions and 2 deletions

View File

@ -3,6 +3,7 @@
#include "VampireGameMode.h" #include "VampireGameMode.h"
#include "Components/CapsuleComponent.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
void AVampireGameMode::BeginPlay() void AVampireGameMode::BeginPlay()
@ -59,6 +60,11 @@ void AVampireGameMode::SpawnEnemy()
FActorSpawnParameters SpawnParameters; FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
AEnemyCharacter* actor = GetWorld()->SpawnActor<AEnemyCharacter>(EnemyTemplate, Transform, SpawnParameters); AEnemyCharacter* Actor = GetWorld()->SpawnActor<AEnemyCharacter>(EnemyTemplate, Transform, SpawnParameters);
actor->SpawnDefaultController(); float CapsuleRadius = Actor->GetCapsuleComponent()->GetScaledCapsuleRadius();
FVector Direction = SpawnLocation - PlayerCharacter->GetActorLocation();
Direction.Normalize();
Direction *= CapsuleRadius;
Actor->SetActorLocation(SpawnLocation + Direction);
Actor->SpawnDefaultController();
} }