Spring arm can now smoothly change socket / target offsets

This commit is contained in:
Steve Streeting 2024-12-06 11:44:47 +00:00
parent 2ecc91f554
commit 394f30251a
2 changed files with 77 additions and 7 deletions

View File

@ -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();
}

View File

@ -21,6 +21,11 @@ public:
protected:
TOptional<float> PrevArmLength;
TOptional<FVector> SmoothTargetOffsetTarget;
float SmoothTargetOffsetSpeed;
TOptional<FVector> 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;
};