MovingPlatform.cpp

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.0 kB
3
Indexable
Never
// 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();
	OriginalLocation = GetActorLocation();
	
}

// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
	
	FVector CurrentLocation = GetActorLocation() + (PlatformVelocity * DeltaTime);
	SetActorLocation(CurrentLocation);

	
	double Distance = FVector::Dist(CurrentLocation, OriginalLocation);

	if(Distance > MoveDistance) {
		FVector MoveDirection = PlatformVelocity.GetSafeNormal();
		OriginalLocation = OriginalLocation + MoveDirection * MoveDistance;
		SetActorLocation(OriginalLocation);

		PlatformVelocity = -PlatformVelocity;
	}
}