From df724c1540c93eb036edf0b9ed0a99a2c0c79030 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 9 Mar 2021 00:18:58 -0500 Subject: [PATCH] `GetForwardLocationWithRandomOffset` is great! --- .../Private/LocationLibrary.cpp | 48 +++++++++++++++++-- .../Public/LocationLibrary.h | 30 ++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Source/ActorSpawningPlugin/Private/LocationLibrary.cpp b/Source/ActorSpawningPlugin/Private/LocationLibrary.cpp index d16e13d..4ae6359 100644 --- a/Source/ActorSpawningPlugin/Private/LocationLibrary.cpp +++ b/Source/ActorSpawningPlugin/Private/LocationLibrary.cpp @@ -1,16 +1,16 @@ #include "LocationLibrary.h" #include "Kismet/GameplayStatics.h" -FVector ULocationLibrary::GetPlayerLocation(UObject* worldContext) +FVector ULocationLibrary::GetPlayerAbsoluteLocation(UObject* worldContext) { APawn* playerPawn = GetPlayerPawn(worldContext); return playerPawn->GetActorLocation(); } -FVector ULocationLibrary::GetPlayerLocationWithOffset(UObject* worldContext, +FVector ULocationLibrary::GetPlayerAbsoluteLocationWithOffset(UObject* worldContext, float x, float y, float z) { - FVector playerLocation = GetPlayerLocation(worldContext); + FVector playerLocation = GetPlayerAbsoluteLocation(worldContext); FVector newLocation( playerLocation.X + x, playerLocation.Y + y, @@ -19,16 +19,54 @@ FVector ULocationLibrary::GetPlayerLocationWithOffset(UObject* worldContext, return newLocation; } -FVector ULocationLibrary::GetPlayerLocationWithRandomOffset(UObject* worldContext, +FVector ULocationLibrary::GetPlayerAbsoluteLocationWithRandomOffset(UObject* worldContext, float minX, float maxX, float minY, float maxY, float minZ, float maxZ) { float x = FMath::RandRange(minX, maxX); float y = FMath::RandRange(minY, maxY); float z = FMath::RandRange(minZ, maxZ); - return GetPlayerLocationWithOffset(worldContext, x, y, z); + return GetPlayerAbsoluteLocationWithOffset(worldContext, x, y, z); } + +FVector ULocationLibrary::GetPlayerForwardLocationWithOffset(UObject* worldContext, + float forwardOffset, + float xAbsoluteOffset, float yAbsoluteOffset, float zAbsoluteOffset) +{ + APawn* playerPawn = GetPlayerPawn(worldContext); + FVector playerLocation = playerPawn->GetActorLocation(); + FVector playerForwardVector = playerPawn->GetActorForwardVector(); + + FVector transformedLocation( + playerLocation.X + xAbsoluteOffset, + playerLocation.Y + yAbsoluteOffset, + playerLocation.Z + zAbsoluteOffset + ); + + FVector scaledForwardVector = playerForwardVector * forwardOffset; + + FVector finalLocation = transformedLocation + scaledForwardVector; + + return finalLocation; +} + +FVector ULocationLibrary::GetPlayerForwardLocationWithRandomOffset(UObject* worldContext, + float minForwardOffset, float maxForwardOffset, + float minAbsoluteXOffset, float maxAbsoluteXOffset, + float minAbsoluteYOffset, float maxAbsoluteYOffset, + float minAbsoluteZOffset, float maxAbsoluteZOffset) +{ + + float forward = FMath::RandRange(minForwardOffset, maxForwardOffset); + float x = FMath::RandRange(minAbsoluteXOffset, maxAbsoluteXOffset); + float y = FMath::RandRange(minAbsoluteYOffset, maxAbsoluteYOffset); + float z = FMath::RandRange(minAbsoluteZOffset, maxAbsoluteZOffset); + + return GetPlayerForwardLocationWithOffset(worldContext, forward, x, y, z); +} + + APawn* ULocationLibrary::GetPlayerPawn(UObject* worldContext) { APawn* playerPawn = UGameplayStatics::GetPlayerPawn(worldContext, 0); diff --git a/Source/ActorSpawningPlugin/Public/LocationLibrary.h b/Source/ActorSpawningPlugin/Public/LocationLibrary.h index 1e41176..a8bb6c1 100644 --- a/Source/ActorSpawningPlugin/Public/LocationLibrary.h +++ b/Source/ActorSpawningPlugin/Public/LocationLibrary.h @@ -20,22 +20,46 @@ class ACTORSPAWNINGPLUGIN_API ULocationLibrary : public UBlueprintFunctionLibrar public: // The player's location + // This has no relation to where the player is looking, but rather the global map. UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions", meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true")) - static FVector GetPlayerLocation(UObject* WorldContextObject); + static FVector GetPlayerAbsoluteLocation(UObject* WorldContextObject); // The player's location with a static offset + // This has no relation to where the player is looking, but rather the global map. UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions", meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true")) - static FVector GetPlayerLocationWithOffset(UObject* WorldContextObject, float x, float y, float z); + static FVector GetPlayerAbsoluteLocationWithOffset(UObject* WorldContextObject, float x, float y, float z); // The player's location with a random offset + // This has no relation to where the player is looking, but rather the global map. UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions", meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true")) - static FVector GetPlayerLocationWithRandomOffset(UObject* WorldContextObject, + static FVector GetPlayerAbsoluteLocationWithRandomOffset(UObject* WorldContextObject, float minX, float maxX, float minY, float maxY, float minZ, float maxZ); + // The player's forward location with a static offset + // This appears "in front" of the player. + // The xyz-offsets are applied before the forward offset. + UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions", + meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true")) + static FVector GetPlayerForwardLocationWithOffset(UObject* WorldContextObject, + float forwardOffset, + float xAbsoluteOffset, float yAbsoluteOffset, float zAbsoluteOffset); + + // The player's forward location with a random offset + // This appears "in front" of the player. + // The xyz-offsets are applied before the forward offset. + UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions", + meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true")) + static FVector GetPlayerForwardLocationWithRandomOffset(UObject* WorldContextObject, + float minForwardOffset, float maxForwardOffset, + float minAbsoluteXOffset, float maxAbsoluteXOffset, + float minAbsoluteYOffset, float maxAbsoluteYOffset, + float minAbsoluteZOffset, float maxAbsoluteZOffset); + + private: static APawn* GetPlayerPawn(UObject* WorldContextObject); };