Skip to content

Commit 4bc43a8

Browse files
committed
feat: Unify camera speed and implement zoom-based movement scaling
1 parent ff822f1 commit 4bc43a8

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

Source/OpenRTSCamera/Private/RTSCamera.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ URTSCamera::URTSCamera()
2626
this->FindGroundTraceLength = 100000;
2727
this->MaximumZoomLength = 5000;
2828
this->MinimumZoomLength = 500;
29-
this->MoveSpeed = 50;
29+
this->MaxMoveSpeed = 1024.0f;
30+
this->MinMoveSpeed = 128.0f;
31+
this->NowMoveSpeed = this->MinMoveSpeed;
3032
this->RotateSpeed = 45;
3133
this->StartingYAngle = -45.0f;
3234
this->StartingZAngle = 0;
@@ -114,6 +116,12 @@ void URTSCamera::OnZoomCamera(const FInputActionValue& Value)
114116
this->MinimumZoomLength,
115117
this->MaximumZoomLength
116118
);
119+
120+
// Calculate Alpha for Lerp (0 at min zoom, 1 at max zoom)
121+
float Alpha = (this->DesiredZoomLength - this->MinimumZoomLength) / (this->MaximumZoomLength - this->MinimumZoomLength);
122+
123+
// Lerp NowMoveSpeed between MinMoveSpeed and MaxMoveSpeed
124+
this->NowMoveSpeed = FMath::Lerp(this->MinMoveSpeed, this->MaxMoveSpeed, Alpha);
117125
}
118126

119127
void URTSCamera::OnRotateCamera(const FInputActionValue& Value)
@@ -228,7 +236,7 @@ void URTSCamera::ApplyMoveCameraCommands()
228236
{
229237
auto Movement = FVector2D(X, Y);
230238
Movement.Normalize();
231-
Movement *= this->MoveSpeed * Scale * this->DeltaSeconds;
239+
Movement *= this->NowMoveSpeed * Scale * this->DeltaSeconds;
232240
this->Root->SetWorldLocation(
233241
this->Root->GetComponentLocation() + FVector(Movement.X, Movement.Y, 0.0f)
234242
);
@@ -424,7 +432,7 @@ void URTSCamera::EdgeScrollLeft() const
424432
const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
425433

426434
this->Root->AddRelativeLocation(
427-
-1 * this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
435+
-1 * this->Root->GetRightVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
428436
);
429437
}
430438

@@ -440,7 +448,7 @@ void URTSCamera::EdgeScrollRight() const
440448

441449
const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
442450
this->Root->AddRelativeLocation(
443-
this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
451+
this->Root->GetRightVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
444452
);
445453
}
446454

@@ -456,7 +464,7 @@ void URTSCamera::EdgeScrollUp() const
456464

457465
const auto Movement = 1 - UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
458466
this->Root->AddRelativeLocation(
459-
this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
467+
this->Root->GetForwardVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
460468
);
461469
}
462470

@@ -472,7 +480,7 @@ void URTSCamera::EdgeScrollDown() const
472480

473481
const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
474482
this->Root->AddRelativeLocation(
475-
-1 * this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
483+
-1 * this->Root->GetForwardVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
476484
);
477485
}
478486

Source/OpenRTSCamera/Public/RTSCamera.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent
6666
float StartingZAngle;
6767

6868
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RTSCamera")
69-
float MoveSpeed;
69+
float MaxMoveSpeed;
70+
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RTSCamera")
71+
float MinMoveSpeed;
7072
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RTSCamera")
7173
float RotateSpeed;
7274

@@ -104,13 +106,7 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent
104106

105107
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "RTSCamera - Edge Scroll Settings")
106108
bool EnableEdgeScrolling;
107-
UPROPERTY(
108-
BlueprintReadWrite,
109-
EditAnywhere,
110-
Category = "RTSCamera - Edge Scroll Settings",
111-
meta=(EditCondition="EnableEdgeScrolling")
112-
)
113-
float EdgeScrollSpeed;
109+
114110
UPROPERTY(
115111
BlueprintReadWrite,
116112
EditAnywhere,
@@ -199,4 +195,6 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent
199195
FVector2D DragStartLocation;
200196
UPROPERTY()
201197
TArray<FMoveCameraCommand> MoveCameraCommands;
198+
UPROPERTY()
199+
float NowMoveSpeed;
202200
};

0 commit comments

Comments
 (0)