Smooth MovingPlatform thanks to Sine function

With a little help from chatGpt I got a nice smooth oscillating platform thanks to the FMath.Sin function.


Here is the code I defined in the header file, just call the function within AMovingPlatform::Tick in the .cpp file:

	FVector StartLocation;
	float OscillationDistance = 1000.0f;
	float OscillationSpeed = 1.0f;

	void MovePlatformBackAndForth()
		float Oscillation = (FMath::Sin(GetWorld()->GetTimeSeconds()) * OscillationSpeed + 1.0f) / 2.0f;
		float MovingDistance = Oscillation * OscillationDistance;

		FVector NewLocation = StartLocation;
		NewLocation.Y += MovingDistance;
1 Like

Very smooth :ok_hand:, ChatGPT can be very useful sometimes.

Privacy & Terms