This project is developed entirely in C++ powered by Unreal Engine.
A set of assets was added, along with C++ code, to make the platform move up, down, left, and right, adding more challenge for the player.
// Fill out your copyright notice in the Description page of Project Settings.
#include "MovingPlatform.h"
// Sets default values
AMovingPlatform::AMovingPlatform()
{
// 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;
}
// Called when the game starts or when spawned
void AMovingPlatform::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation();
}
// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MovePlatform(DeltaTime);
RotatePlatform(DeltaTime);
}
void AMovingPlatform::MovePlatform(float DeltaTime)
{
if (shouldPlatformReturn())
{
FVector MoveDirection = PlatformVelocity.GetSafeNormal();
StartLocation = StartLocation + MoveDirection * MovedDistance;
SetActorLocation(StartLocation);
PlatformVelocity = -PlatformVelocity;
}
else
{
FVector CurrentLocation = GetActorLocation();
CurrentLocation = CurrentLocation + PlatformVelocity * DeltaTime;
SetActorLocation(CurrentLocation);
}
}
void AMovingPlatform::RotatePlatform(float DeltaTime)
{
AddActorLocalRotation(RotationSpeed * DeltaTime);
}
bool AMovingPlatform::shouldPlatformReturn() const
{
return GetDistanceMoved() > MovedDistance;
}
float AMovingPlatform::GetDistanceMoved() const
{
return FVector::Dist(StartLocation, GetActorLocation());
}