What I ended up with

This is an awesome cause so far, this is what I came up with building on what I have before so initial calculations in local space at begin play and store the start and target world space locations then. then just bounce between them with VInterpConstantTo and checking once they are equal (or add tolerance to check which can give fun results when using VInterpTo could add some randomisation to the tolerance or expose that as a parameter to tweak as well so some of the platforms pause etc. at the ends. Seems to work!

Edit ended up adding a scale multiplication as well so if I scaled the actors when setting up the scene the end location remained correct.

From
TargetVector = (LSTargetLocation - LSCurrentLocation);

to

TargetVector = ((LSTargetLocation * GetActorScale()) - LSCurrentLocation);
#include "MovingPlatform.h"

AMovingPlatform::AMovingPlatform()
{
	//Constructor
	PrimaryActorTick.bCanEverTick = true;
	SetMobility(EComponentMobility::Movable);
}

void AMovingPlatform::BeginPlay()
{
	Super::BeginPlay();
	//do it on begin play not construct as construct can happen too early.
	if (HasAuthority())
	{
		SetReplicates(true);
		SetReplicateMovement(true);

		//CurrentLocation will be 0,0,0 in local Space
		TargetVector = ((LSTargetLocation * GetActorScale()) - LSCurrentLocation);

		//Get the noirmalised Direction and also how long the vector is as a float
		TargetVector.ToDirectionAndLength(NormalisedVectorDirection, VectorLength);

		WSStartLocation = GetActorLocation();
		//Add World Location offset of Actor Root being in local space.
		WSTargetLocation = NormalisedVectorDirection * VectorLength + WSStartLocation;

	}

}

void AMovingPlatform::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (HasAuthority())
	{
		WorldLocation = GetActorLocation();

		//Constent Lerp to location Speed is UPROPERTY
		//FMath::VInterpTo for nice fade to stop perhaps use later VInterpConstantTo
		if (MoveToTarget)
		{
			LerpLocation = FMath::VInterpConstantTo(WorldLocation, WSTargetLocation, DeltaTime, Speed);
			//Check if at target, if it is go back.
			if (WorldLocation.Equals(WSTargetLocation, 1))
			{
				MoveToTarget = false;
			}
		}
		else
		{
			LerpLocation = FMath::VInterpConstantTo(WorldLocation, WSStartLocation, DeltaTime, Speed);
			//tolorance is fun if used with VInterpTo can use it to pause and spring away again, leave at 1 for normal
				//Check if at target, if it is go back.
			if (WorldLocation.Equals(WSStartLocation, 1))
			{
				MoveToTarget = true;
			}
		}

		SetActorLocation(LerpLocation);
	}
}
1 Like

Privacy & Terms