Jump to content
  • Advertisement
Sign in to follow this  
Devin Ferriera

Trying to change speed of a platform

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!