From 74882e634bb741bb28807fe0209be27cefae8db8 Mon Sep 17 00:00:00 2001 From: xoiox Date: Wed, 29 Apr 2026 09:01:04 +0200 Subject: [PATCH] climbing mode polished --- .../MainCharacter/CHBP_MainCharacter.uasset | 4 +- .../customCharacterMovementComponent.cpp | 138 ++++++++++-------- .../Public/customCharacterMovementComponent.h | 1 + 3 files changed, 79 insertions(+), 64 deletions(-) diff --git a/Content/lostPlanet/MainCharacter/CHBP_MainCharacter.uasset b/Content/lostPlanet/MainCharacter/CHBP_MainCharacter.uasset index 8534b63..6ec601b 100644 --- a/Content/lostPlanet/MainCharacter/CHBP_MainCharacter.uasset +++ b/Content/lostPlanet/MainCharacter/CHBP_MainCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:dd5b78baf3941012dd3fc89b061d6c59c40d8e7cc3e2e14c80eec3db1834af8a -size 498174 +oid sha256:a0bee979ad76c765088c67253f79ad4d0da35e187b90afac6634f05bbdebbcad +size 500273 diff --git a/Source/lost_planet/Private/customCharacterMovementComponent.cpp b/Source/lost_planet/Private/customCharacterMovementComponent.cpp index 634129e..76fefc1 100644 --- a/Source/lost_planet/Private/customCharacterMovementComponent.cpp +++ b/Source/lost_planet/Private/customCharacterMovementComponent.cpp @@ -20,68 +20,7 @@ void UCustomCharacterMovementComponent::PhysCustom(float DeltaTime, int32 Iterat { case 0: { - if (DeltaTime < MIN_TICK_TIME) - { - return; - } - - RestorePreAdditiveRootMotionVelocity(); - - if (!HasAnimRootMotion() && !CurrentRootMotion.HasOverrideVelocity()) - { - if (bCheatFlying && Acceleration.IsZero()) - { - Velocity = FVector::ZeroVector; - } - //const float Friction = 0.5f * GetPhysicsVolume()->FluidFriction; - constexpr float Friction = 5.f; - CalcVelocity(DeltaTime, Friction, true, GetMaxBrakingDeceleration()); - } - - ApplyRootMotionToVelocity(DeltaTime); - - bJustTeleported = false; - - FVector OldLocation = UpdatedComponent->GetComponentLocation(); - const FVector Adjusted = Velocity * DeltaTime; - FHitResult Hit(1.f); - SafeMoveUpdatedComponent(Adjusted, UpdatedComponent->GetComponentQuat(), true, Hit); - - if (Hit.Time < 1.f) - { - SlideAlongSurface(Velocity * DeltaTime, 1.f - Hit.Time, Hit.Normal, Hit, true); - } - - const ACharacter* Character = Cast(GetOwner()); - FHitResult WallHit; - const bool bWallFound = SweepForwardForWall(Character, WallHit); - if (bWallFound) - { - const FVector SurfaceNormal = WallHit.Normal; - - // Update Location - const FVector TargetLocation = WallHit.Location + (SurfaceNormal * 0.1f); - const FVector NewLocation = FMath::VInterpTo(UpdatedComponent->GetComponentLocation(), TargetLocation, DeltaTime, 2.f); - UpdatedComponent->SetWorldLocation(NewLocation); - - // Update Rotation - const FRotator TargetRotation = FRotationMatrix::MakeFromXZ(-SurfaceNormal, FVector::UpVector).Rotator(); - const FRotator NewRotation = FMath::RInterpTo(UpdatedComponent->GetComponentRotation(), TargetRotation, DeltaTime, 2.f); - UpdatedComponent->SetWorldRotation(NewRotation); - } - else - { - // Reset Rotation and set MovementMode to Falling - const FRotator NewRotation = FRotator(0.0f, UpdatedComponent->GetComponentRotation().Yaw, 0.0f); - UpdatedComponent->SetWorldRotation(NewRotation); - SetMovementMode(MOVE_Falling); - } - - if (!bJustTeleported && !HasAnimRootMotion() && !CurrentRootMotion.HasOverrideVelocity()) - { - Velocity = (UpdatedComponent->GetComponentLocation() - OldLocation) / DeltaTime; - } - + PhysClimbing(DeltaTime, Iterations); break; } @@ -93,6 +32,81 @@ void UCustomCharacterMovementComponent::PhysCustom(float DeltaTime, int32 Iterat } } +void UCustomCharacterMovementComponent::PhysClimbing(float DeltaTime, int32 Iterations) +{ + if (DeltaTime < MIN_TICK_TIME) + { + return; + } + + RestorePreAdditiveRootMotionVelocity(); + + if (!HasAnimRootMotion() && !CurrentRootMotion.HasOverrideVelocity()) + { + if (bCheatFlying && Acceleration.IsZero()) + { + Velocity = FVector::ZeroVector; + } + //const float Friction = 0.5f * GetPhysicsVolume()->FluidFriction; + constexpr float Friction = 5.f; + CalcVelocity(DeltaTime, Friction, true, GetMaxBrakingDeceleration()); + } + + ApplyRootMotionToVelocity(DeltaTime); + + bJustTeleported = false; + + FVector OldLocation = UpdatedComponent->GetComponentLocation(); + const FVector Adjusted = Velocity * DeltaTime; + FHitResult Hit(1.f); + SafeMoveUpdatedComponent(Adjusted, UpdatedComponent->GetComponentQuat(), true, Hit); + + if (Hit.Time < 1.f) + { + SlideAlongSurface(Velocity * DeltaTime, 1.f - Hit.Time, Hit.Normal, Hit, true); + } + + const ACharacter* Character = Cast(GetOwner()); + FHitResult WallHit; + const bool bWallFound = SweepForwardForWall(Character, WallHit); + if (bWallFound) + { + const FVector SurfaceNormal = WallHit.Normal; + + // Update Location + const FVector TargetLocation = WallHit.Location + (SurfaceNormal * 0.1f); + const FVector NewLocation = FMath::VInterpTo(UpdatedComponent->GetComponentLocation(), TargetLocation, DeltaTime, 2.f); + UpdatedComponent->SetWorldLocation(NewLocation); + + // Update Rotation + FRotator TargetRotation = FRotationMatrix::MakeFromXZ(-SurfaceNormal, FVector::UpVector).Rotator(); + if (TargetRotation.Pitch < -70.0f) + { + TargetRotation = FRotator(0.0f, UpdatedComponent->GetComponentRotation().Yaw, 0.0f); + UpdatedComponent->SetWorldRotation(TargetRotation); + SetMovementMode(MOVE_Falling); + GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Standing up")); + } + else + { + const FRotator NewRotation = FMath::RInterpTo(UpdatedComponent->GetComponentRotation(), TargetRotation, DeltaTime, 2.f); + UpdatedComponent->SetWorldRotation(NewRotation); + } + } + else + { + // Reset Rotation and set MovementMode to Falling + const FRotator NewRotation = FRotator(0.0f, UpdatedComponent->GetComponentRotation().Yaw, 0.0f); + UpdatedComponent->SetWorldRotation(NewRotation); + SetMovementMode(MOVE_Falling); + } + + if (!bJustTeleported && !HasAnimRootMotion() && !CurrentRootMotion.HasOverrideVelocity()) + { + Velocity = (UpdatedComponent->GetComponentLocation() - OldLocation) / DeltaTime; + } +} + void UCustomCharacterMovementComponent::OnMovementModeChanged(EMovementMode PreviousMovementMode, uint8 PreviousCustomMode) { Super::OnMovementModeChanged(PreviousMovementMode, PreviousCustomMode); diff --git a/Source/lost_planet/Public/customCharacterMovementComponent.h b/Source/lost_planet/Public/customCharacterMovementComponent.h index d87bfd9..d62d683 100644 --- a/Source/lost_planet/Public/customCharacterMovementComponent.h +++ b/Source/lost_planet/Public/customCharacterMovementComponent.h @@ -21,6 +21,7 @@ public: protected: // Override PhysCustom to handle custom movement mode behaviors virtual void PhysCustom(float DeltaTime, int32 Iterations) override; + void PhysClimbing(float DeltaTime, int32 Iterations); virtual void OnMovementModeChanged(EMovementMode PreviousMovementMode, uint8 PreviousCustomMode) override; static bool SweepForwardForWall(const ACharacter* Character, FHitResult& OutHit, float SweepDistance = 100.f); };