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-
100105void 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
119130void 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
0 commit comments