From 12291c9b190efc5e3a0bd8c6fbb4386d09bbf9da Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 4 Apr 2025 22:57:43 +0100 Subject: [PATCH] Constrain player to viewable area --- Source/vampires/PlayerCharacter.cpp | 23 +++++++++++++++++++++++ Source/vampires/PlayerCharacter.h | 1 + 2 files changed, 24 insertions(+) diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index 9797413..7ff91b1 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -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; diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index caac86c..2be31e7 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -38,6 +38,7 @@ protected: virtual void BeginPlay() override; public: + virtual void Tick(float DeltaTime) override; UEXPComponent* GetEXPComponent();