Working on positionable crosshair
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
#include "UtilityLibrary.h"
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "EnhancedInputSubsystems.h"
|
||||
#include "Kismet/KismetSystemLibrary.h"
|
||||
#include "Kismet/GameplayStatics.h"
|
||||
|
||||
|
||||
#define LOCTEXT_NAMESPACE "Luprex Utility"
|
||||
|
||||
@@ -73,3 +76,102 @@ UEnhancedInputLocalPlayerSubsystem *UlxUtilityLibrary::GetEnhancedInputLocalPlay
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// bool UlxUtilityLibrary::LineTraceMultipleLines(const AActor* ReferenceActor, const FVector &Offset,
|
||||
// const TArray<FlxTraceStartTraceEnd> &TraceStartAndTraceEnd, ETraceTypeQuery TraceChannel,
|
||||
// bool bTraceComplex, const TArray<AActor*>& ActorsToIgnore, EDrawDebugTrace::Type DrawDebugType, FHitResult& OutHit, bool bIgnoreReferenceActor,
|
||||
// FLinearColor TraceColor, FLinearColor TraceHitColor, float DrawTime)
|
||||
// {
|
||||
// OutHit.Init();
|
||||
// OutHit.Distance = FLT_MAX;
|
||||
// FTransform ReferenceTransform = ReferenceActor->GetActorTransform();
|
||||
|
||||
// for (const FlxTraceStartTraceEnd &Points : TraceStartAndTraceEnd) {
|
||||
// FHitResult OneResult;
|
||||
// FVector TraceStart = ReferenceTransform.TransformPosition(Points.TraceStart + Offset);
|
||||
// FVector TraceEnd = ReferenceTransform.TransformPosition(Points.TraceEnd + Offset);
|
||||
// bool Found = UKismetSystemLibrary::LineTraceSingle(ReferenceActor, TraceStart, TraceEnd, TraceChannel, bTraceComplex,
|
||||
// ActorsToIgnore, DrawDebugType, OneResult, bIgnoreReferenceActor, TraceColor, TraceHitColor, DrawTime);
|
||||
// if (Found && (OneResult.Distance < OutHit.Distance))
|
||||
// {
|
||||
// OutHit = OneResult;
|
||||
// }
|
||||
// }
|
||||
// if (OutHit.Distance == FLT_MAX) {
|
||||
// OutHit.Distance = 0.0;
|
||||
// return false;
|
||||
// } else {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
|
||||
bool UlxUtilityLibrary::LineTraceThroughMousePointer(const APlayerController* PlayerController,
|
||||
EMouseSpecificationType MouseSpecification, FVector2D ManualMouseXY, double MaxDistanceFromCamera,
|
||||
ETraceTypeQuery TraceChannel, bool bTraceComplex, EDrawDebugTrace::Type DrawDebugType, bool bIgnorePlayerPawn,
|
||||
const TArray<AActor*>& ActorsToIgnore, AActor *& Actor, FHitResult& HitResult)
|
||||
{
|
||||
const FLinearColor TraceColor = FLinearColor::Red;
|
||||
const FLinearColor TraceHitColor = FLinearColor::Green;
|
||||
const double DrawTime = 1.0;
|
||||
|
||||
// Zero out the return values.
|
||||
Actor = nullptr;
|
||||
HitResult.Init();
|
||||
|
||||
// Sanity check the distance.
|
||||
if (MaxDistanceFromCamera <= 0.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure there's a player pawn.
|
||||
AActor *PlayerPawn = PlayerController->GetPawn();
|
||||
if (!PlayerPawn) return false;
|
||||
|
||||
// Get the Mouse XY.
|
||||
FVector2D MouseXY;
|
||||
switch (MouseSpecification)
|
||||
{
|
||||
case EMouseSpecificationType::GetMouseAutomatically:
|
||||
{
|
||||
double X, Y;
|
||||
if (!PlayerController->GetMousePosition(X, Y)) return false;
|
||||
MouseXY = FVector2D(X, Y);
|
||||
break;
|
||||
}
|
||||
case EMouseSpecificationType::SpecifyMouseInPixels:
|
||||
{
|
||||
MouseXY = ManualMouseXY;
|
||||
break;
|
||||
}
|
||||
case EMouseSpecificationType::SpecifyMouseAsZeroToOne:
|
||||
{
|
||||
int32 VX, VY;
|
||||
PlayerController->GetViewportSize(VX, VY);
|
||||
if ((VX == 0) || (VY == 0)) return false;
|
||||
MouseXY = ManualMouseXY * FVector2D(VX, VY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the trace start and trace end positions in world space.
|
||||
FVector WorldStart, WorldDirection, WorldEnd;
|
||||
if (!UGameplayStatics::DeprojectScreenToWorld(PlayerController, MouseXY, WorldStart, WorldDirection))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
WorldEnd = WorldStart + (WorldDirection * MaxDistanceFromCamera);
|
||||
|
||||
// Find the hit.
|
||||
if (UKismetSystemLibrary::LineTraceSingle(PlayerPawn, WorldStart, WorldEnd, TraceChannel, bTraceComplex,
|
||||
ActorsToIgnore, DrawDebugType, HitResult, bIgnorePlayerPawn, TraceColor, TraceHitColor, DrawTime))
|
||||
{
|
||||
Actor = HitResult.GetActor();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fail.
|
||||
Actor = nullptr;
|
||||
HitResult.Init();
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user