# Updsting RTS Camera of Rama/ Please Check ?

**URL:** <https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 15, 2015, 8:10pm UTC](https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529 "2015-04-15T20:10:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TAN](https://forums.unrealengine.com/u/TAN)\
**Post date:** [April 15, 2015, 8:10pm UTC](https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529/1 "2015-04-15T20:10:42Z")

</div>

Hey mates.

I have been going on Rama’s great tutorial of a RTS camera tutorial for my own game here:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

I think I manged to update it 4.7.5. At least it compiles without errors. But I am still a newbie. Can you mates check it too and help me if it works for you too before telling Rama about it ?

Thanks everyone 🙂

Oh, also I changed the Pawn class’ name from FESpectatorPawn to RTSCamera. Thanks again 😉

RTSCamera.h

```auto

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Pawn.h"
#include "RTSCamera.generated.h"

/** AFESpectatorPawn
* This Pawn Will Move Like An RTS Camera
*/

UCLASS()
class RTS_API ARTSCamera : public APawn
{
	GENERATED_BODY()

	ARTSCamera(const FObjectInitializer& ObjectInitializer);

public:

	/** Camera Component */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	UCameraComponent* CameraComponent;

	/** Camera Z Angle */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraZAnlge;

	/** Camera Radius From Pawn Position */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraRadius;

	/** Camera Height Angle */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraHeightAngle;

	/** Camera Zoom Speed */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraZoomSpeed;

	/** Camera Radius Max */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraRadiusMax;

	/** Camera Radius Min */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraRadiusMin;

	/** Camera Movement Speed */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraMovementSpeed;

	/** Camera Scroll Boundary */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float CameraScrollBoundary;

	/** Should the camera move? */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	bool bCanMoveCamera;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera)
	bool bAddDefaultMovementBindings;

private:
	/** Sets up player inputs
	* @param InputComponent - Input Component
	*/
	void SetupPlayerInputComponent(class UInputComponent* InputComponent);

public:

	UFUNCTION()
	void BeginPlay();

	/** Zooms In The Camera */
	UFUNCTION()
	void ZoomIn();

	/** Zooms Out The Camera */
	UFUNCTION()
	void ZoomOut();

	/** Gets the roatation of the camera with only the yaw value
	* @return - returns a rotator that is (0, yaw, 0) of the Camera
	*/
	UFUNCTION()
	FRotator GetIsolatedCameraYaw();

	/** Moves the camera forward
	* @param direcation - (1.0 for forward, -1.0 for backward)
	*/
	UFUNCTION()
	void MoveCameraForward(float direction);

	/** Moves the camera forward
	* @param direcation - (1.0 for right, -1.0 for left)
	*/
	UFUNCTION()
	void MoveCameraRight(float direction);

	/** Repositions The Camera */
	UFUNCTION()
	void RepositionCamera();

	/** Tick Function, Called Every Frame */
	UFUNCTION()
	virtual void Tick(float DeltaSeconds) OVERRIDE;
	
	
};

```

RTSCamera.cpp

```auto

// Fill out your copyright notice in the Description page of Project Settings.

#include "RTS.h"
#include "RTSCamera.h"

// Sets default values
ARTSCamera::ARTSCamera(const class FObjectInitializer& PCIP)

{
	//Disable Standard WASD Movement
	bAddDefaultMovementBindings = false;

	//Set Default Camera Values
	CameraRadius = 1000.0f;
	CameraZAnlge = 0.0f;
	CameraHeightAngle = 70.0f;
	CameraZoomSpeed = 32.0f;
	CameraRadiusMin = 750.0f;
	CameraRadiusMax = 2000.0f;
	CameraMovementSpeed = 2000.0f;
	CameraScrollBoundary = 25.0f;
	//TODO: While selecting units, the camera CANNOT move!
	bCanMoveCamera = true;

	//Intialize The Camera
	CameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("RTS Camera"));
	CameraComponent->AttachParent = this->GetRootComponent();
	CameraComponent->bUsePawnControlRotation = false;
	RepositionCamera();

	//Enable Tick function
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ARTSCamera::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ARTSCamera::Tick( float DeltaSeconds )
{

	Super::Tick(DeltaSeconds);

	//Create variables to hold mouse position and screen size
	FVector2D mousePosition;
	FVector2D viewportSize;

	//Get mouse position and screen size
	UGameViewportClient* gameViewport = GEngine->GameViewport;

	//Make sure viewport exists
	check(gameViewport);
	gameViewport->GetViewportSize(viewportSize);

	//Make sure the viewport has focus(contains the mouse)
	if (gameViewport->IsFocused(gameViewport->Viewport) && gameViewport->GetMousePosition(mousePosition) && bCanMoveCamera)
	{
		//Check if the mouse is at the left or right edge of the screen and move accordingly
		if (mousePosition.X < CameraScrollBoundary)
		{
			MoveCameraRight(-1.0f * DeltaSeconds);
		}
		else if (viewportSize.X - mousePosition.X < CameraScrollBoundary)
		{
			MoveCameraRight(1.0f * DeltaSeconds);
		}

		//Check if the mouse is at the top or bottom edge of the screen and move accordingly
		if (mousePosition.Y < CameraScrollBoundary)
		{
			MoveCameraForward(1.0f * DeltaSeconds);
		}
		else if (viewportSize.Y - mousePosition.Y < CameraScrollBoundary)
		{
			MoveCameraForward(-1.0f * DeltaSeconds);
		}
	}

}

// Called to bind functionality to input
void ARTSCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	check(InputComponent);

	//Bind Mouse Wheel Zooming Actions
	InputComponent->BindAction("WheelMouseUp", IE_Pressed, this, &ARTSCamera::ZoomIn);
	InputComponent->BindAction("WheelMouseDown", IE_Pressed, this, &ARTSCamera::ZoomOut);

	//Bind WASD Movement
	//FOR TESTING PURPOSES ONLY!!!
	//InputComponent->BindAxis("MoveForward", this, &AFESpectatorPawn::MoveCameraForward);
	//InputComponent->BindAxis("MoveRight", this, &AFESpectatorPawn::MoveCameraRight);

}

void ARTSCamera::ZoomIn()
{
	//Don't execute any further if the camera can't move
	if (!bCanMoveCamera)
	return;

	//Decrease the CameraRadius but clamp it between the min and max radii
	CameraRadius = FMath::Clamp(CameraRadius - CameraZoomSpeed, CameraRadiusMin, CameraRadiusMax);

	//Reposition the camera in the local space
	RepositionCamera();
}

void ARTSCamera::ZoomOut()
{
	//Don't execute any further if the camera can't move
	if (!bCanMoveCamera)
	return;

	//Increase the CameraRadius but clamp it between the min and max radii
	CameraRadius = FMath::Clamp(CameraRadius + CameraZoomSpeed, CameraRadiusMin, CameraRadiusMax);

	//Reposition the camera in the local space
	RepositionCamera();
}

FRotator ARTSCamera::GetIsolatedCameraYaw()
{
	//Return a FRotator containing (0, CameraYaw, 0)
	return FRotator(0.0f, CameraComponent->ComponentToWorld.Rotator().Yaw, 0.0f);
}

void ARTSCamera::MoveCameraForward(float direction)
{
	//Don't execute any further if the camera can't move
	if (!bCanMoveCamera)
	return;

	//Calculate how much to move the camera by
	float movementValue = direction * CameraMovementSpeed;

	//Create a delta vector that moves by the movementValue in the direction of the camera's yaw
	FVector deltaMovement = movementValue * GetIsolatedCameraYaw().Vector();

	//Add the delta to a new vector
	FVector newLocation = this->GetActorLocation() + deltaMovement;

	//Set the new location of the pawn
	SetActorLocation(newLocation);
}

void ARTSCamera::MoveCameraRight(float direction)
{
	//Don't execute any further if the camera can't move
	if (!bCanMoveCamera)
	return;

	//Calculate how much to move the camera by
	float movementValue = direction * CameraMovementSpeed;

	//Create a delta vector that moves by the movementValue in the direction of the right of the camera's yaw
	FVector deltaMovement = movementValue * (FRotator(0.0f, 90.0f, 0.0f) + GetIsolatedCameraYaw()).Vector();

	//Add the delta to a new vector
	FVector newLocation = this->GetActorLocation() + deltaMovement;

	//Set the new location of the pawn
	SetActorLocation(newLocation);
}

void ARTSCamera::RepositionCamera()
{
	//Create variables to hold the new values
	FVector newLocation(0.0f, 0.0f, 0.0f);
	FRotator newRotation(0.0f, 0.0f, 0.0f);

	//Find Cos and Sin of the Camera Z Angle
	float sinCameraZAngle = FMath::Sin(FMath::DegreesToRadians(CameraZAnlge));
	float cosCameraZAngle = FMath::Cos(FMath::DegreesToRadians(CameraZAnlge));

	//Find the Cos and Sin of the Camera Height Angle
	float sinCameraHeightAngle = FMath::Sin(FMath::DegreesToRadians(CameraHeightAngle));
	float cosCameraHeightAngle = FMath::Cos(FMath::DegreesToRadians(CameraHeightAngle));

	//Set newLocation X to cosCameraZAngle * sinCameraHeightAngle * CameraRadius
	newLocation.X = cosCameraZAngle * cosCameraHeightAngle * CameraRadius;

	//Set newLocation Y to sinCameraZangle * sinCameraHeightAngle * CameraRadius
	newLocation.Y = sinCameraZAngle * cosCameraHeightAngle * CameraRadius;

	//Set newLocation Z to cosCameraHeightAngle * CameraRadius
	newLocation.Z = sinCameraHeightAngle * CameraRadius;

	//Set the new rotations
	newRotation = (FVector(0.0f, 0.0f, 0.0f) - newLocation).Rotation();

	//Set the camera's location and rotation to the new values
	CameraComponent->SetRelativeLocation(newLocation);
	CameraComponent->SetRelativeRotation(newRotation);
}

```

\*\* EDIT:\*\*

By the way since I am a novice… errm… now I have written this… how do I use this ? 😃

Yeah so I have to make something in level blueprint or something to use that camera ? 😃 😃 😃

Sorry 😃

---

<div class="post-metadata">

**Author:** ![TAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TAN](https://forums.unrealengine.com/u/TAN)\
**Post date:** [April 16, 2015, 12:58pm UTC](https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529/2 "2015-04-16T12:58:12Z")

</div>

Uhhm… Anyone ? 🙂

---

<div class="post-metadata">

**Author:** ![CBrew](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CBrew](https://forums.unrealengine.com/u/CBrew)\
**Post date:** [April 18, 2015, 8:58pm UTC](https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529/3 "2015-04-18T20:58:16Z")

</div>

Hello There! I am the author of that tutorial. Once you have that created you need to set the default pawn in the game mode to the RTSCamera class.  
Hope this helps!

---

<div class="post-metadata">

**Author:** ![TAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TAN](https://forums.unrealengine.com/u/TAN)\
**Post date:** [April 19, 2015, 8:08pm UTC](https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529/4 "2015-04-19T20:08:53Z")

</div>

> [@CBrew;274596](#):
>
> Hello There! I am the author of that tutorial.

![anime.kon.05.5.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/0/6/064a759546a80cc0954fecbae655f717ca05f225.jpeg)

Thanks mate. Sorry for the blunder. I am new at this stuff 🙂

---

<div class="post-metadata">

**Author:** ![TAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TAN](https://forums.unrealengine.com/u/TAN)\
**Post date:** [April 20, 2015, 8:23pm UTC](https://forums.unrealengine.com/t/updsting-rts-camera-of-rama-please-check/27529/5 "2015-04-20T20:23:56Z")

</div>

I managed it. But none of the controls works. Only a camera and no movement. No WASD control or mouse camera.

You think something wrong with the code ? Can you also copy/paste and try this code ? Here is a screen. Thanks

 ![As.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/a/6/a650fab8ceabfe2be0f96fe46ecbff77fff9398b.jpeg)
