# Trying to change speed of a platform

This topic is 1035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So this is the current code I am using to make a platform move in Unreal 4 Engine. I not really sure what is controlling the speed in this code. I am trying to make a platforming game as a personal project just to get myself familiar with unreal 4. I have gotten other platforms to move just have not figured out how to set the speed of them. Here is the code I'm using and am open to suggestions on possibly a way to make it better.

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

FVector PlatformLocation = GetActorLocation();

float PlatformDistance = (FMath::Sin( PlatformTime + DeltaTime ) - FMath::Sin(PlatformTime));

PlatformLocation.X += PlatformDistance * 500.0F;   //Moves platform Forward and Backwards, This will also determine distance.

PlatformTime += DeltaTime * .02;

SetActorLocation(PlatformLocation);

}

##### Share on other sites
While there isn’t anything wrong with relative moving, you may want to have a reference point, such that you don’t accumulate floating point errors. Additionally, you try to understand the units in the math involved, rather than using magic numbers:

// Assuming the platform has these properties:
Float3  Start;
Float3  End;
Float   Duration; // seconds it takes for platform to cycle from start and end

// And:
Float3  Delta = End - Start;

// then:
// “phase” cycles between 0 and 1 over “Duration” seconds
phase = (1+sin(PlatformTime / Duration * 2*pi))/2;

// “PlatformLocation” cycles between “Start” and “End” according to “phase”
PlatformLocation = Start + Delta * phase;

PlatformTime += DeltaTime;

Edited by fastcall22

##### Share on other sites

While there isn’t anything wrong with relative moving, you may want to have a reference point, such that you don’t accumulate floating point errors. Additionally, you try to understand the units in the math involved, rather than using magic numbers:

// “phase” cycles between -1 and 1 over “Duration” seconds
phase = sin(PlatformTime / Duration * 2*pi);

// “PlatformLocation” cycles between “Anchor” ± “Delta” according to “phase”
PlatformLocation = Anchor + Delta * phase;

PlatformTime += DeltaTime;


So change float PlatformDistance = (FMath::Sin( PlatformTime + DeltaTime ) - FMath::Sin(PlatformTime));

PlatformLocation.X += PlatformDistance * 500.0F;   //Moves platform Forward and Backwards, This will also determine distance.

PlatformTime += DeltaTime * .02;

for what you gave me?

##### Share on other sites

Yeah. Comment out your code. Paste his. Then see what happens.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634146
• Total Posts
3015772
×