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 "Character/KOFBaseCharacter.h"
#include "GameModes/KOFDefaultGameMode.h"
#include "Kismet/GameplayStatics.h"
AKOFDefaultCamera::AKOFDefaultCamera()
{
@ -41,8 +42,9 @@ void AKOFDefaultCamera::Tick(float DeltaSeconds)
FVector P1Loc = P1->GetActorLocation();
FVector P2Loc = P2->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);
// set FOV

View File

@ -40,37 +40,11 @@ void UKOFCharacterMovementComponent::TickComponent(float DeltaTime, ELevelTick T
FVector CurrentLoc = Owner->GetActorLocation();
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);
FVector CurrentLoc = Owner->GetActorLocation();
Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z));
}
// clamp to stage bounds
FVector CurrentLoc = Owner->GetActorLocation();
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;
MaxAllowedDistanceFromOpponent = 700.0f;
StageLength = 2400.0f;
StartY = 250.0f;
}

View File

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

View File

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