From 394f30251a023ef5c74e4e3b56c1e4728950b66f Mon Sep 17 00:00:00 2001 From: Steve Streeting Date: Fri, 6 Dec 2024 11:44:47 +0000 Subject: [PATCH] Spring arm can now smoothly change socket / target offsets --- .../Private/StevesSpringArmComponent.cpp | 55 +++++++++++++++++-- .../Public/StevesSpringArmComponent.h | 29 +++++++++- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/Source/StevesUEHelpers/Private/StevesSpringArmComponent.cpp b/Source/StevesUEHelpers/Private/StevesSpringArmComponent.cpp index b3ce1fa..3fb01d6 100644 --- a/Source/StevesUEHelpers/Private/StevesSpringArmComponent.cpp +++ b/Source/StevesUEHelpers/Private/StevesSpringArmComponent.cpp @@ -9,11 +9,36 @@ UStevesSpringArmComponent::UStevesSpringArmComponent(): bEnableSmoothCollisionAv { } -FVector UStevesSpringArmComponent::BlendLocations(const FVector& DesiredArmLocation, - const FVector& TraceHitLocation, - bool bHitSomething, +void UStevesSpringArmComponent::UpdateDesiredArmLocation(bool bDoTrace, + bool bDoLocationLag, + bool bDoRotationLag, float DeltaTime) { + // Smooth target & socket offsets + if (SmoothTargetOffsetTarget.IsSet()) + { + TargetOffset = FMath::VInterpTo(TargetOffset, SmoothTargetOffsetTarget.GetValue(), DeltaTime, SmoothTargetOffsetSpeed); + if (TargetOffset.Equals(SmoothTargetOffsetTarget.GetValue())) + { + CancelTargetOffsetSmooth(); + } + } + if (SmoothSocketOffsetTarget.IsSet()) + { + SocketOffset = FMath::VInterpTo(SocketOffset, SmoothSocketOffsetTarget.GetValue(), DeltaTime, SmoothSocketOffsetSpeed); + if (SocketOffset.Equals(SmoothSocketOffsetTarget.GetValue())) + { + CancelSocketOffsetSmooth(); + } + } + Super::UpdateDesiredArmLocation(bDoTrace, bDoLocationLag, bDoRotationLag, DeltaTime); +} + +FVector UStevesSpringArmComponent::BlendLocations(const FVector& DesiredArmLocation, + const FVector& TraceHitLocation, + bool bHitSomething, + float DeltaTime) +{ #if WITH_EDITORONLY_DATA && ENABLE_VISUAL_LOG if (bHitSomething && bVisualLogCameraCollision) @@ -49,7 +74,7 @@ FVector UStevesSpringArmComponent::BlendLocations(const FVector& DesiredArmLocat Ret += FRotationMatrix(PreviousDesiredRot).TransformVector(SocketOffset); PrevArmLength = NewArmLen; - + return Ret; } else @@ -58,3 +83,25 @@ FVector UStevesSpringArmComponent::BlendLocations(const FVector& DesiredArmLocat } } + +void UStevesSpringArmComponent::SetTargetOffsetSmooth(const FVector& NewTargetOffset, float Speed) +{ + SmoothTargetOffsetTarget = NewTargetOffset; + SmoothTargetOffsetSpeed = Speed; +} + +void UStevesSpringArmComponent::CancelTargetOffsetSmooth() +{ + SmoothTargetOffsetTarget.Reset(); +} + +void UStevesSpringArmComponent::SetSocketOffsetSmooth(const FVector& NewSocketOffset, float Speed) +{ + SmoothSocketOffsetTarget = NewSocketOffset; + SmoothSocketOffsetSpeed = Speed; +} + +void UStevesSpringArmComponent::CancelSocketOffsetSmooth() +{ + SmoothSocketOffsetTarget.Reset(); +} diff --git a/Source/StevesUEHelpers/Public/StevesSpringArmComponent.h b/Source/StevesUEHelpers/Public/StevesSpringArmComponent.h index 2179215..4e8ba6b 100644 --- a/Source/StevesUEHelpers/Public/StevesSpringArmComponent.h +++ b/Source/StevesUEHelpers/Public/StevesSpringArmComponent.h @@ -21,6 +21,11 @@ public: protected: TOptional PrevArmLength; + TOptional SmoothTargetOffsetTarget; + float SmoothTargetOffsetSpeed; + TOptional SmoothSocketOffsetTarget; + float SmoothSocketOffsetSpeed; + #if WITH_EDITORONLY_DATA UPROPERTY(Transient, EditAnywhere, Category=CameraCollision) bool bVisualLogCameraCollision = false; @@ -29,10 +34,28 @@ protected: public: UStevesSpringArmComponent(); + /// Smoothly change the target offset, instead of jumping + UFUNCTION(BlueprintCallable, Category="SpringArm") + void SetTargetOffsetSmooth(const FVector& NewTargetOffset, float Speed = 5); + /// Interrupt a smooth target offset change, freeze where we are + UFUNCTION(BlueprintCallable, Category="SpringArm") + void CancelTargetOffsetSmooth(); + + /// Smoothly change the socket offset, instead of jumping + UFUNCTION(BlueprintCallable, Category="SpringArm") + void SetSocketOffsetSmooth(const FVector& NewSocketOffset, float Speed = 5); + /// Interrupt a smooth socket offset change, freeze where we are + UFUNCTION(BlueprintCallable, Category="SpringArm") + void CancelSocketOffsetSmooth(); + protected: - virtual FVector BlendLocations(const FVector& DesiredArmLocation, - const FVector& TraceHitLocation, - bool bHitSomething, + virtual void UpdateDesiredArmLocation(bool bDoTrace, + bool bDoLocationLag, + bool bDoRotationLag, float DeltaTime) override; + virtual FVector BlendLocations(const FVector& DesiredArmLocation, + const FVector& TraceHitLocation, + bool bHitSomething, + float DeltaTime) override; };