Updsting RTS Camera of Rama/ Please Check ?

Hey mates.

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

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 :slight_smile:

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

RTSCamera.h




// 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




// 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 ? :smiley:

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

Sorry :smiley:

Uhhm… Anyone ? :slight_smile:

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!

anime.kon.05.5.jpg

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

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