[C++ VIDEO TUTORIAL] - Making a simple "Subway Surfer" game

This short tutorial is a follow-up from my “Temple Run” tutorial which you can find over here:

It shows how to add collectable items and it also shows how to change the player movement to be like that in games such as “Subway Surfer”.

Here is the code for the “good” items which the player can collect:


#pragma once

#include "GameFramework/Actor.h"
#include "GoodItem.generated.h"

class SKYRUN_API AGoodItem : public AActor
	/** Sphere collision component */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Enemy, meta = (AllowPrivateAccess = "true"))
	UStaticMeshComponent* CollectableMeshComponent;

	USphereComponent *myCollisionSphere;

	// Sets default values for this actor's properties

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	void HandleCollision(AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& OverlapInfo);


#include "SkyRun.h"
#include "GoodItem.h"

// Sets default values
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	myCollisionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionSphere"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> GoodMesh(TEXT("/Game/StarterContent/Shapes/Shape_Torus.Shape_Torus"));
	// Create the mesh component
	CollectableMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("GoodMesh"));
	RootComponent = myCollisionSphere;

	CollectableMeshComponent->SetWorldScale3D(FVector(.5f, .5f, .5f));


	myCollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &AGoodItem::HandleCollision);

// Called when the game starts or when spawned
void AGoodItem::BeginPlay()

// Called every frame
void AGoodItem::Tick( float DeltaTime )
	Super::Tick( DeltaTime );


void AGoodItem::HandleCollision(AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& OverlapInfo)
	FString name = Other->GetName();
	if (!name.Contains("ThirdPersonCharacter"))

	UWorld* const World = GetWorld();
	if (World)


and in SkyRunCharacter.cpp, you can use code like this to make the player move left and right when the A and D key are pressed:

void ASkyRunCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)

InputComponent->BindAxis(“MoveRight”, this, &ASkyRunCharacter::ChangeDirection);


and then

void ASkyRunCharacter::**ChangeDirection**(float Value)
	if (Value != 0 && canTurnRight) {
		canTurnRight = false;

		if (Value == 1)
			SetActorLocation(GetActorLocation() + FVector(0, 400, 0));//move to the right
		else if (Value == -1)
			SetActorLocation(GetActorLocation() + FVector(0, -400, 0));//move to the left
	else if (Value == 0)
		canTurnRight = true;


Note that if the player is moving along a spline, then the left and right movement will need to be along the right / left vector which depends on the player’s rotation at any given time. Then you can use

FTransform ft = SplineIterator->GetTransformAtDistanceAlongSpline(length, ESplineCoordinateSpace::World);
FVector right = ft.GetRotation().GetAxisY();

and add or subtract a multiple of that vector to move the player right and left.

That’s what I did for the roller coaster movement here:

Hope it helps.

GOD Bless you man…

Thank you man!!!