Skip to content

Commit 0127273

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

5 files changed

Lines changed: 36 additions & 26 deletions

File tree

Content/BP_RTSCamera.uasset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:ad0d80f14898ade77d58d545aeb3150e1acc8287d0322c24aa7442cd98dea54e
3-
size 25195
2+
oid sha256:0d2e201517cc64e3e555b53bdec3f1705cb8590341e6f9d4c0f9b7448cd39a6c
3+
size 27303

OpenRTSCamera.uplugin

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
{
1+
{
22
"CanContainContent": true,
33
"Category": "Gameplay",
44
"CreatedBy": "Jesus Bracho",
55
"CreatedByURL": "https://github.com/HeyZoos",
66
"Description": "An Unreal Engine 5 open-source RTS/MOBA camera implementation that aims to be fully featured, customizable and dependable",
77
"DocsURL": "https://github.com/HeyZoos/OpenRTSCamera/wiki",
88
"EnabledByDefault": true,
9-
"EngineVersion": "5.3.0",
9+
"EngineVersion": "5.5.0",
1010
"FileVersion": 3,
1111
"FriendlyName": "OpenRTSCamera",
1212
"Installed": false,
@@ -32,4 +32,4 @@
3232
"SupportURL": "https://github.com/HeyZoos/OpenRTSCamera/issues",
3333
"Version": 1,
3434
"VersionName": "0.21.0"
35-
}
35+
}

Source/OpenRTSCamera/Private/RTSCamera.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Jesus Bracho All Rights Reserved.
1+
// Copyright 2024 Jesus Bracho All Rights Reserved.
22

33
#include "RTSCamera.h"
44

@@ -17,7 +17,6 @@ URTSCamera::URTSCamera()
1717
this->CameraBlockingVolumeTag = FName("OpenRTSCamera#CameraBounds");
1818
this->CollisionChannel = ECC_WorldStatic;
1919
this->DragExtent = 0.6f;
20-
this->EdgeScrollSpeed = 50;
2120
this->DistanceFromEdgeThreshold = 0.1f;
2221
this->EnableCameraLag = true;
2322
this->EnableCameraRotationLag = true;
@@ -26,7 +25,9 @@ URTSCamera::URTSCamera()
2625
this->FindGroundTraceLength = 100000;
2726
this->MaximumZoomLength = 5000;
2827
this->MinimumZoomLength = 500;
29-
this->MoveSpeed = 50;
28+
this->MaxMoveSpeed = 1024.0f;
29+
this->MinMoveSpeed = 128.0f;
30+
this->NowMoveSpeed = this->MinMoveSpeed; // Initialize NowMoveSpeed to MinMoveSpeed
3031
this->RotateSpeed = 45;
3132
this->StartingYAngle = -45.0f;
3233
this->StartingZAngle = 0;
@@ -74,6 +75,8 @@ void URTSCamera::BeginPlay()
7475
this->CheckForEnhancedInputComponent();
7576
this->BindInputMappingContext();
7677
this->BindInputActions();
78+
79+
7780
}
7881
}
7982

@@ -88,6 +91,9 @@ void URTSCamera::TickComponent(
8891
if (NetMode != NM_DedicatedServer && this->PlayerController->GetViewTarget() == this->Owner)
8992
{
9093
this->DeltaSeconds = DeltaTime;
94+
95+
96+
9197
this->ApplyMoveCameraCommands();
9298
this->ConditionallyPerformEdgeScrolling();
9399
this->ConditionallyKeepCameraAtDesiredZoomAboveGround();
@@ -96,7 +102,6 @@ void URTSCamera::TickComponent(
96102
this->ConditionallyApplyCameraBounds();
97103
}
98104
}
99-
100105
void URTSCamera::FollowTarget(AActor* Target)
101106
{
102107
this->CameraFollowTarget = Target;
@@ -114,6 +119,12 @@ void URTSCamera::OnZoomCamera(const FInputActionValue& Value)
114119
this->MinimumZoomLength,
115120
this->MaximumZoomLength
116121
);
122+
123+
// Calculate Alpha for Lerp (0 at min zoom, 1 at max zoom)
124+
float Alpha = (this->DesiredZoomLength - this->MinimumZoomLength) / (this->MaximumZoomLength - this->MinimumZoomLength);
125+
126+
// Lerp NowMoveSpeed between MinMoveSpeed and MaxMoveSpeed
127+
this->NowMoveSpeed = FMath::Lerp(this->MinMoveSpeed, this->MaxMoveSpeed, Alpha);
117128
}
118129

119130
void URTSCamera::OnRotateCamera(const FInputActionValue& Value)
@@ -228,7 +239,7 @@ void URTSCamera::ApplyMoveCameraCommands()
228239
{
229240
auto Movement = FVector2D(X, Y);
230241
Movement.Normalize();
231-
Movement *= this->MoveSpeed * Scale * this->DeltaSeconds;
242+
Movement *= this->NowMoveSpeed * Scale * this->DeltaSeconds;
232243
this->Root->SetWorldLocation(
233244
this->Root->GetComponentLocation() + FVector(Movement.X, Movement.Y, 0.0f)
234245
);
@@ -424,7 +435,7 @@ void URTSCamera::EdgeScrollLeft() const
424435
const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
425436

426437
this->Root->AddRelativeLocation(
427-
-1 * this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
438+
-1 * this->Root->GetRightVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
428439
);
429440
}
430441

@@ -440,7 +451,7 @@ void URTSCamera::EdgeScrollRight() const
440451

441452
const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
442453
this->Root->AddRelativeLocation(
443-
this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
454+
this->Root->GetRightVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
444455
);
445456
}
446457

@@ -456,7 +467,7 @@ void URTSCamera::EdgeScrollUp() const
456467

457468
const auto Movement = 1 - UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
458469
this->Root->AddRelativeLocation(
459-
this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
470+
this->Root->GetForwardVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
460471
);
461472
}
462473

@@ -472,7 +483,7 @@ void URTSCamera::EdgeScrollDown() const
472483

473484
const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0);
474485
this->Root->AddRelativeLocation(
475-
-1 * this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds
486+
-1 * this->Root->GetForwardVector() * Movement * this->NowMoveSpeed * this->DeltaSeconds
476487
);
477488
}
478489

Source/OpenRTSCamera/Public/RTSCamera.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Copyright 2024 Jesus Bracho All Rights Reserved.
1+
// Copyright 2024 Jesus Bracho All Rights Reserved.
22

33
#pragma once
44

5-
#include "CoreMinimal.h"
5+
#include <CoreMinimal.h>
66
#include "InputMappingContext.h"
77
#include "Camera/CameraComponent.h"
88
#include "Components/ActorComponent.h"
@@ -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,8 @@ 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+
110+
114111
UPROPERTY(
115112
BlueprintReadWrite,
116113
EditAnywhere,
@@ -199,4 +196,6 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent
199196
FVector2D DragStartLocation;
200197
UPROPERTY()
201198
TArray<FMoveCameraCommand> MoveCameraCommands;
199+
UPROPERTY()
200+
float NowMoveSpeed;
202201
};

Source/OpenRTSCamera/Public/RTSSelector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Copyright 2024 Jesus Bracho All Rights Reserved.
1+
// Copyright 2024 Jesus Bracho All Rights Reserved.
22

33
#pragma once
44

5-
#include "CoreMinimal.h"
5+
#include <CoreMinimal.h>
66
#include "InputAction.h"
77
#include "InputMappingContext.h"
88
#include "RTSHUD.h"

0 commit comments

Comments
 (0)