Add inertia to Pawn movement

I’ve just started to learn Unreal Engine 4.26.2 while developing an Atari’s Pong clone.

To move the Paddle I’m using this code:

// Called to bind functionality to input
void APaddle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	// Respond every frame to the values of our movement.
	InputComponent->BindAxis(TEXT("MovePaddle"), this, &APaddle::Move_ZAxis);
}

void APaddle::Move_ZAxis(float AxisValue)
{
	CurrentVelocity.Z = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

But I want to do two things:

  1. While the user is pressing the key the paddle movement will accelerate.

  2. When the user release the key the Paddle won’t stop immediately; instead, it will slowing down until it stops.

Is there anything done with Physics that I can use here or do I have to implement it by myself?

By the way, I’m not English and I don’t know if inertia is the right word here.

I haven’t implemented custom movement in Unreal before, but I’ve spent a lot of time with these in Unity. Since the movement in Pong has to be responsive, I’d go with the following solution: You have your velocity, that’s good. Instead of setting the velocity directly, you set a target velocity, and interpolate (smoothly reach) the current v. to the target v. continuously.

// in input code
TargetVelocity.Z = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;

// in tick (so every frame)
// higher interp speed will result in more responsive movement
CurrentVelocity.Z = FMath::FInterpTo(CurrentVelocity.Z, TargetVelocity.Z, DeltaSeconds, InterpSpeed);
1 Like

Thanks for your answer but it can be achieved using Physics?

As in using forces? Of course, but there is no point really. IMO it’s better to control the velocity directly when dealing with character/player movement. But if you want to use forces no matter what, then use AddForce() and play around with the mass and linear damping values of the physics body.

1 Like