Constrain player to viewable area

This commit is contained in:
baz 2025-04-04 22:57:43 +01:00
parent 64a21df059
commit 12291c9b19
2 changed files with 24 additions and 0 deletions

View File

@ -11,6 +11,7 @@
#include "InputMappingContext.h"
#include "WeaponInventoryComponent.h"
#include "Components/WidgetComponent.h"
#include "Kismet/GameplayStatics.h"
APlayerCharacter::APlayerCharacter()
{
@ -34,6 +35,28 @@ void APlayerCharacter::BeginPlay()
Super::BeginPlay();
}
void APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
auto PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
FVector TopLeft, TopLeftDir;
FVector BottomRight, BottomRightDir;
FVector2d ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir);
auto location = GetActorLocation();
location.X = FMath::Clamp(location.X, BottomRight.X, TopLeft.X);
location.Y = FMath::Clamp(location.Y, TopLeft.Y, BottomRight.Y);
SetActorLocation(location);
}
UEXPComponent* APlayerCharacter::GetEXPComponent()
{
return EXPComponent;

View File

@ -38,6 +38,7 @@ protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
UEXPComponent* GetEXPComponent();