mirror of
https://github.com/storytold/ActorSpawningPlugin.git
synced 2026-10-09 00:09:44 +00:00
GetForwardLocationWithRandomOffset is great!
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
#include "LocationLibrary.h"
|
#include "LocationLibrary.h"
|
||||||
#include "Kismet/GameplayStatics.h"
|
#include "Kismet/GameplayStatics.h"
|
||||||
|
|
||||||
FVector ULocationLibrary::GetPlayerLocation(UObject* worldContext)
|
FVector ULocationLibrary::GetPlayerAbsoluteLocation(UObject* worldContext)
|
||||||
{
|
{
|
||||||
APawn* playerPawn = GetPlayerPawn(worldContext);
|
APawn* playerPawn = GetPlayerPawn(worldContext);
|
||||||
return playerPawn->GetActorLocation();
|
return playerPawn->GetActorLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
FVector ULocationLibrary::GetPlayerLocationWithOffset(UObject* worldContext,
|
FVector ULocationLibrary::GetPlayerAbsoluteLocationWithOffset(UObject* worldContext,
|
||||||
float x, float y, float z)
|
float x, float y, float z)
|
||||||
{
|
{
|
||||||
FVector playerLocation = GetPlayerLocation(worldContext);
|
FVector playerLocation = GetPlayerAbsoluteLocation(worldContext);
|
||||||
FVector newLocation(
|
FVector newLocation(
|
||||||
playerLocation.X + x,
|
playerLocation.X + x,
|
||||||
playerLocation.Y + y,
|
playerLocation.Y + y,
|
||||||
@@ -19,16 +19,54 @@ FVector ULocationLibrary::GetPlayerLocationWithOffset(UObject* worldContext,
|
|||||||
return newLocation;
|
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 minX, float maxX, float minY, float maxY, float minZ, float maxZ)
|
||||||
{
|
{
|
||||||
float x = FMath::RandRange(minX, maxX);
|
float x = FMath::RandRange(minX, maxX);
|
||||||
float y = FMath::RandRange(minY, maxY);
|
float y = FMath::RandRange(minY, maxY);
|
||||||
float z = FMath::RandRange(minZ, maxZ);
|
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* ULocationLibrary::GetPlayerPawn(UObject* worldContext)
|
||||||
{
|
{
|
||||||
APawn* playerPawn = UGameplayStatics::GetPlayerPawn(worldContext, 0);
|
APawn* playerPawn = UGameplayStatics::GetPlayerPawn(worldContext, 0);
|
||||||
|
|||||||
@@ -20,22 +20,46 @@ class ACTORSPAWNINGPLUGIN_API ULocationLibrary : public UBlueprintFunctionLibrar
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// The player's location
|
// The player's location
|
||||||
|
// This has no relation to where the player is looking, but rather the global map.
|
||||||
UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions",
|
UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions",
|
||||||
meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true"))
|
meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true"))
|
||||||
static FVector GetPlayerLocation(UObject* WorldContextObject);
|
static FVector GetPlayerAbsoluteLocation(UObject* WorldContextObject);
|
||||||
|
|
||||||
// The player's location with a static offset
|
// 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",
|
UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions",
|
||||||
meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true"))
|
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
|
// 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",
|
UFUNCTION(BlueprintPure, Category = "SpawningPluginLocationFunctions",
|
||||||
meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true"))
|
meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true"))
|
||||||
static FVector GetPlayerLocationWithRandomOffset(UObject* WorldContextObject,
|
static FVector GetPlayerAbsoluteLocationWithRandomOffset(UObject* WorldContextObject,
|
||||||
float minX, float maxX, float minY,
|
float minX, float maxX, float minY,
|
||||||
float maxY, float minZ, float maxZ);
|
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:
|
private:
|
||||||
static APawn* GetPlayerPawn(UObject* WorldContextObject);
|
static APawn* GetPlayerPawn(UObject* WorldContextObject);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user