From 8d8b7fa88ccd75cfd7fd47eb8aa482f08b435189 Mon Sep 17 00:00:00 2001 From: Kevin Poretti Date: Sat, 7 Jan 2023 18:09:12 -0500 Subject: [PATCH] Ground movement now works properly for all gravity directions --- .../Content/Characters/BP_GSCharacter.uasset | 4 +- .../GSCharacterMovementComponent.cpp | 42 +++++++++++++++++-- .../Character/GSCharacterMovementComponent.h | 4 ++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/GravityStomp/Content/Characters/BP_GSCharacter.uasset b/GravityStomp/Content/Characters/BP_GSCharacter.uasset index 13b7e85..03533d0 100644 --- a/GravityStomp/Content/Characters/BP_GSCharacter.uasset +++ b/GravityStomp/Content/Characters/BP_GSCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:35e5001ea477718971711cf88b2302fb8c88f318cfb4bbe64295b02f7f75b1d8 -size 105153 +oid sha256:7df60ef1396a72d6de3e1eb6bd2bb71a675116f902d1fe4a81f8b091f17c4415 +size 105966 diff --git a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp index e3447ce..3e08d9b 100644 --- a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp +++ b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp @@ -45,7 +45,7 @@ void UGSCharacterMovementComponent::PhysFalling(float deltaTime, int32 Iteration } FVector FallAcceleration = GetFallingLateralAcceleration(deltaTime); - FallAcceleration.Z = 0.f; + IsCharacterUpAlignedToWorldUp() ? FallAcceleration.Z = 0.f : FallAcceleration.Y = 0.f; const bool bHasLimitedAirControl = ShouldLimitAirControl(deltaTime, FallAcceleration); float remainingTime = deltaTime; @@ -359,10 +359,25 @@ void UGSCharacterMovementComponent::PhysFalling(float deltaTime, int32 Iteration } } +FVector UGSCharacterMovementComponent::GetFallingLateralAcceleration(float DeltaTime) +{ + // No acceleration in Z or Y depending on character up direction + FVector FallAcceleration = IsCharacterUpAlignedToWorldUp() ? FVector(Acceleration.X, Acceleration.Y, 0.f) : FVector(Acceleration.X, 0.0f, Acceleration.Z); + + // bound acceleration, falling object has minimal ability to impact acceleration + if (!HasAnimRootMotion() && FallAcceleration.SizeSquared() > 0.f) + { + FallAcceleration = GetAirControl(DeltaTime, AirControl, FallAcceleration); + FallAcceleration = FallAcceleration.GetClampedToMaxSize(GetMaxAcceleration()); + } + + return FallAcceleration; +} + void UGSCharacterMovementComponent::ComputeFloorDist(const FVector& CapsuleLocation, float LineDistance, - float SweepDistance, FFindFloorResult& OutFloorResult, float SweepRadius, - const FHitResult* DownwardSweepResult) const + float SweepDistance, FFindFloorResult& OutFloorResult, float SweepRadius, + const FHitResult* DownwardSweepResult) const { //UE_LOG(LogCharacterMovement, VeryVerbose, TEXT("[Role:%d] ComputeFloorDist: %s at location %s"), (int32)CharacterOwner->GetLocalRole(), *GetNameSafe(CharacterOwner), *CapsuleLocation.ToString()); OutFloorResult.Clear(); @@ -650,6 +665,27 @@ void UGSCharacterMovementComponent::SetCharacterUpDirection(FVector NewUpDirecti CharacterUpDirection = NewUpDirection; } +FVector UGSCharacterMovementComponent::ConstrainInputAcceleration(const FVector& InputAcceleration) const +{ + // walking or falling pawns ignore up/down sliding + if(IsCharacterUpAlignedToWorldUp()) + { + if (InputAcceleration.Z != 0.f && (IsMovingOnGround() || IsFalling())) + { + return FVector(InputAcceleration.X, InputAcceleration.Y, 0.f); + } + } + else + { + if (InputAcceleration.Y != 0.f && (IsMovingOnGround() || IsFalling())) + { + return FVector(0.0f, InputAcceleration.Y, InputAcceleration.Z); + } + } + + return InputAcceleration; +} + void UGSCharacterMovementComponent::OnMovementModeChanged(EMovementMode PreviousMovementMode, uint8 PreviousCustomMode) { if (!HasValidData()) diff --git a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h index e027b33..ac1169a 100644 --- a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h +++ b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h @@ -24,6 +24,8 @@ public: // UCharacterMovementComponent interface + virtual FVector ConstrainInputAcceleration(const FVector& InputAcceleration) const override; + virtual void OnMovementModeChanged(EMovementMode PreviousMovementMode, uint8 PreviousCustomMode) override; // walking @@ -42,6 +44,8 @@ public: // falling virtual void PhysFalling(float deltaTime, int32 Iterations) override; + virtual FVector GetFallingLateralAcceleration(float DeltaTime) override; + virtual void ComputeFloorDist(const FVector& CapsuleLocation, float LineDistance, float SweepDistance, FFindFloorResult& OutFloorResult, float SweepRadius, const FHitResult* DownwardSweepResult) const override; virtual bool IsValidLandingSpot(const FVector& CapsuleLocation, const FHitResult& Hit) const override;