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.

movingPlatform

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

private:
	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;
		SetActorLocation(NewLocation);
	}
1 Like

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

Privacy & Terms