It is called from Blueprints on Tick (for testing purposes)
Header
// © 2017 Tyler Barrett. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "TraceFromPlayerLocation.generated.h"
UENUM()
enum ETraceDirection {Up, Down, Front, Back, Left, Right};
UENUM()
enum EDrawDebugLine {None, Persistent, OneFrame, Duration};
UCLASS()
class PLAYERTRACE_API UTraceFromPlayerLocation : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// Draws the debug line during a line trace.
static void DrawLineTraceDebugger(TEnumAsByte<EDrawDebugLine> DrawDebugLineSetting, double DebugDuration,
const bool bHit, const AActor* WorldContextObject, FVector Start, FVector End,
FHitResult OutHit, FLinearColor TraceColor, FLinearColor HitColor);
// Draws the debug line during a sphere trace.
static void DrawSphereTraceDebugger(TEnumAsByte<EDrawDebugLine> DrawDebugSphereSetting, double DebugDuration,
const double Radius, const bool bHit, const AActor* WorldContextObject, FVector Start, FVector End,
FHitResult OutHit, FLinearColor TraceColor, FLinearColor HitColor);
// Performs a single line trace by channel that starts from the player location.
UFUNCTION(BlueprintPure, Category = "Player Trace", meta = (AutoCreateRefTerm = "ActorsToIgnore", DefaultToSelf = "WorldContextObject", HidePin="WorldContextObject", AdvancedDisplay = 9))
static void SingleLineTraceFromPlayer(bool& bHit, FHitResult& OutHit, const double Distance, const TEnumAsByte<ETraceDirection> Direction,
const TEnumAsByte<ETraceTypeQuery> CollisionChannel, const bool bTraceComplex, TArray<AActor*> ActorsToIgnore, bool IgnoreSelf = true,
const TEnumAsByte<EDrawDebugLine> DrawDebugLineSettings = None, double DebugDuration = 0.5, FLinearColor TraceColor = FLinearColor::Red, FLinearColor HitColor = FLinearColor::Green,
const AActor* WorldContextObject = nullptr);
// Performs a single line trace by channel that starts from the cameras location and goes in the direction the camera is facing.
UFUNCTION(BlueprintPure, Category = "Player Trace", meta = (AutoCreateRefTerm = "ActorsToIgnore", DefaultToSelf = "WorldContextObject", HidePin="WorldContextObject", AdvancedDisplay = 9))
static void SingleLineTraceFromCamera(bool& bHit, FHitResult& OutHit, const double Distance,
const TEnumAsByte<ETraceTypeQuery> CollisionChannel, const bool bTraceComplex, TArray<AActor*> ActorsToIgnore, bool IgnoreSelf = true,
const TEnumAsByte<EDrawDebugLine> DrawDebugLineSettings = None, double DebugDuration = 0.5, FLinearColor TraceColor = FLinearColor::Red, FLinearColor HitColor = FLinearColor::Green,
const AActor* WorldContextObject = nullptr);
// Performs a single sphere trace by channel that starts from the players location.
UFUNCTION(BlueprintPure, Category = "Player Trace", meta = (AutoCreateRefTerm = "ActorsToIgnore", DefaultToSelf = "WorldContextObject", HidePin="WorldContextObject", AdvancedDisplay = 9))
static void SingleSphereTraceFromPlayer(bool& bHit, FHitResult& OutHit, const double Distance, const TEnumAsByte<ETraceDirection> Direction, const double Radius,
const TEnumAsByte<ETraceTypeQuery> CollisionChannel, const bool bTraceComplex, TArray<AActor*> ActorsToIgnore, bool IgnoreSelf = true,
const TEnumAsByte<EDrawDebugLine> DrawDebugSphereSettings = None, double DebugDuration = 0.5, FLinearColor TraceColor = FLinearColor::Red, FLinearColor HitColor = FLinearColor::Green,
const AActor* WorldContextObject = nullptr);
};
CPP
// Performs a single sphere trace by channel that starts from the player location.
void UTraceFromPlayerLocation::SingleSphereTraceFromPlayer(bool& bHit, FHitResult& OutHit, const double Distance,
const TEnumAsByte<ETraceDirection> Direction, const double Radius,
const TEnumAsByte<ETraceTypeQuery> CollisionChannel, const bool bTraceComplex, TArray<AActor*> ActorsToIgnore,
bool IgnoreSelf, const TEnumAsByte<EDrawDebugLine> DrawDebugSphereSettings, double DebugDuration,
FLinearColor TraceColor, FLinearColor HitColor, const AActor* WorldContextObject)
{
// Creates a reference to the player and creates the start/stop vectors for the line trace.
const ACharacter* PlayerRef = UGameplayStatics::GetPlayerCharacter(WorldContextObject->GetWorld(), 0);
const FVector Start = PlayerRef->GetActorLocation();
FVector End = FVector(0.0, 0.0, 0.0);
// Sets the end of the line trace to be the correct distance and direction away from the player.
switch (Direction)
{
case Up:
End = Start + (PlayerRef->GetActorUpVector() * Distance);
break;
case Down:
End = Start + -(PlayerRef->GetActorUpVector() * Distance);
break;
case Front:
End = Start + (PlayerRef->GetActorForwardVector() * Distance);
break;
case Back:
End = Start + -(PlayerRef->GetActorForwardVector() * Distance);
break;
case Right:
End = Start + (PlayerRef->GetActorRightVector() * Distance);
break;
case Left:
End = Start + -(PlayerRef->GetActorRightVector() * Distance);
break;
default: //This should never run!
GEngine->AddOnScreenDebugMessage(1, 10.0, FColor::Orange, TEXT("WARNING: SingleLineTraceFromPlayer did not find a direction to trace in!"));
}
// Creates the collision channel, query parameters, and hit result for the line trace.
ECollisionChannel TraceChannel = UEngineTypes::ConvertToCollisionChannel(CollisionChannel);
FCollisionQueryParams TraceParams;
FHitResult TraceHitResult;
// Adds ignored actors to the query parameters.
if(IgnoreSelf)
TraceParams.AddIgnoredActor(PlayerRef);
if(ActorsToIgnore.Num() > 0)
TraceParams.AddIgnoredActors(ActorsToIgnore);
// Performs the sphere trace and returns the hit result.
bHit = UKismetSystemLibrary::SphereTraceSingle(WorldContextObject, Start, End, Radius, CollisionChannel, bTraceComplex, ActorsToIgnore, EDrawDebugTrace::None, OutHit, IgnoreSelf, TraceColor, HitColor, DebugDuration);
OutHit = TraceHitResult;
DrawSphereTraceDebugger(DrawDebugSphereSettings, DebugDuration, Radius, bHit, WorldContextObject, Start, End, OutHit, TraceColor, HitColor);
void UTraceFromPlayerLocation::DrawLineTraceDebugger(TEnumAsByte<EDrawDebugLine> DrawDebugLineSetting, double DebugDuration,
const bool bHit, const AActor* WorldContextObject, FVector Start, FVector End,
FHitResult OutHit, FLinearColor TraceColor, FLinearColor HitColor)
{
// Initializes Debug Settings.
bool bPersistentDebug;
switch (DrawDebugLineSetting)
{
case None:
return;
case Persistent:
bPersistentDebug = true;
break;
case OneFrame:
bPersistentDebug = false;
DebugDuration = 0.0;
break;
case Duration:
bPersistentDebug = false;
break;
default: //This should never run!
GEngine->AddOnScreenDebugMessage(1, 10.0, FColor::Orange, TEXT("WARNING: SingleLineTraceFromPlayer did not find a debug mode!"));
}
// Draws Debug Information.
if(bHit)
{
DrawDebugLine(WorldContextObject->GetWorld(), Start, OutHit.ImpactPoint, TraceColor.ToFColorSRGB(), bPersistentDebug, DebugDuration, 0, 1.0);
DrawDebugLine(WorldContextObject->GetWorld(), OutHit.ImpactPoint, End, HitColor.ToFColorSRGB(), bPersistentDebug, DebugDuration, 0, 1.0);
DrawDebugSphere(WorldContextObject->GetWorld(), OutHit.ImpactPoint, 2.0, 12, HitColor.ToFColorSRGB(), bPersistentDebug, DebugDuration, 0, 1.0);
}
else
{
DrawDebugLine(WorldContextObject->GetWorld(), Start, End, TraceColor.ToFColorSRGB(), bPersistentDebug, DebugDuration, 0, 1.0);
}
}```