Clamp character and camera position to stage boundaries

Clamp character and camera position to stage boundaries
This commit is contained in:
Kevin Poretti 2022-01-17 14:56:25 -05:00
parent 0942a3a1c6
commit 6d3b189234
6 changed files with 19 additions and 39 deletions

BIN
KOFForever/Content/Maps/DebugStage.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -7,6 +7,7 @@
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Character/KOFBaseCharacter.h" #include "Character/KOFBaseCharacter.h"
#include "GameModes/KOFDefaultGameMode.h" #include "GameModes/KOFDefaultGameMode.h"
#include "Kismet/GameplayStatics.h"
AKOFDefaultCamera::AKOFDefaultCamera() AKOFDefaultCamera::AKOFDefaultCamera()
{ {
@ -42,7 +43,8 @@ void AKOFDefaultCamera::Tick(float DeltaSeconds)
FVector P2Loc = P2->GetActorLocation(); FVector P2Loc = P2->GetActorLocation();
FVector CameraLoc = GetActorLocation(); FVector CameraLoc = GetActorLocation();
FVector MidPoint = FVector(CameraLoc.X, (P1Loc.Y + P2Loc.Y) / 2.0f, ((P1Loc.Z + P2Loc.Z) / ZAveragingFactor) + HeightOffset); float HalfStageLength = GameMode->GetStageLength() / 2.0f;
FVector MidPoint = FVector(CameraLoc.X, FMath::Clamp((P1Loc.Y + P2Loc.Y) / 2.0f, -HalfStageLength, HalfStageLength), ((P1Loc.Z + P2Loc.Z) / ZAveragingFactor) + HeightOffset);
SetActorLocation(MidPoint); SetActorLocation(MidPoint);
// set FOV // set FOV

View File

@ -40,37 +40,11 @@ void UKOFCharacterMovementComponent::TickComponent(float DeltaTime, ELevelTick T
FVector CurrentLoc = Owner->GetActorLocation(); FVector CurrentLoc = Owner->GetActorLocation();
Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z)); Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z));
} }
}
void UKOFCharacterMovementComponent::PostPhysicsTickComponent(float DeltaTime,
FCharacterMovementComponentPostPhysicsTickFunction& ThisTickFunction)
{
Super::PostPhysicsTickComponent(DeltaTime, ThisTickFunction);
AKOFBaseCharacter* Owner = Cast<AKOFBaseCharacter>(GetOwner());
if(!Owner)
{
UE_LOG(LogTemp, Error, TEXT("AKOFCharacterMovementComponent::PostPhysicsTickComponent - could not get the owner"));
return;
}
AKOFDefaultGameMode* GameMode = Cast<AKOFDefaultGameMode>(GetWorld()->GetAuthGameMode());
if(!GameMode)
{
UE_LOG(LogTemp, Error, TEXT("AKOFCharacterMovementComponent::PostPhysicsTickComponent - could not get the game mode"));
return;
}
AKOFBaseCharacter* Opponent = GameMode->GetCurrentOpponent(Owner->IsPossessedByPlayer1());
if(Opponent)
{
float YDistance = FMath::Abs(Opponent->GetActorLocation().Y - Owner->GetActorLocation().Y);
float HalfYDistance = YDistance / 2.0f;
float YMidpoint = (Owner->GetActorLocation().Y + Opponent->GetActorLocation().Y) / 2.0f;
float AdjustedY = FMath::Clamp(Owner->GetActorLocation().Y, YMidpoint - HalfYDistance, YMidpoint + HalfYDistance);
// clamp to stage bounds
FVector CurrentLoc = Owner->GetActorLocation(); FVector CurrentLoc = Owner->GetActorLocation();
Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z)); float HalfStageLength = GameMode->GetStageLength() / 2.0f;
} Owner->SetActorLocation(FVector(CurrentLoc.X,
FMath::Clamp(CurrentLoc.Y, -HalfStageLength, HalfStageLength),
CurrentLoc.Z));
} }

View File

@ -10,6 +10,7 @@ AKOFDefaultGameMode::AKOFDefaultGameMode()
NumCharactersPerTeam = 3; NumCharactersPerTeam = 3;
MaxAllowedDistanceFromOpponent = 700.0f; MaxAllowedDistanceFromOpponent = 700.0f;
StageLength = 2400.0f;
StartY = 250.0f; StartY = 250.0f;
} }

View File

@ -17,6 +17,4 @@ class KOFFOREVER_API UKOFCharacterMovementComponent : public UCharacterMovementC
UKOFCharacterMovementComponent(); UKOFCharacterMovementComponent();
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void PostPhysicsTickComponent(float DeltaTime, FCharacterMovementComponentPostPhysicsTickFunction& ThisTickFunction) override;
}; };

View File

@ -33,6 +33,8 @@ public:
FORCEINLINE float GetMaxAllowedDistanceFromOpponent() { return MaxAllowedDistanceFromOpponent; } FORCEINLINE float GetMaxAllowedDistanceFromOpponent() { return MaxAllowedDistanceFromOpponent; }
FORCEINLINE float GetStageLength() { return StageLength; }
protected: protected:
/** Number of characters on a single player's team */ /** Number of characters on a single player's team */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Settings|Team") UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Settings|Team")
@ -42,6 +44,9 @@ protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
float MaxAllowedDistanceFromOpponent; float MaxAllowedDistanceFromOpponent;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
float StageLength;
/** Y offset from origin where character's will be spawned */ /** Y offset from origin where character's will be spawned */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
float StartY; float StartY;