1
\$\begingroup\$

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

This is how I have implemented the Paddle's movement (but this implementation doesn't accelerate or has inertia):

// 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.

\$\endgroup\$
2
  • \$\begingroup\$ What key are you using to control the paddle? If it's a keyboard key then "AxisValue" will always be 1 so the "CurrentVelocity" will be constant. \$\endgroup\$
    – Stephen
    May 24, 2021 at 8:43
  • \$\begingroup\$ @Stephen Thanks for your comment. Please, re-read the question again carefully. I know that my solution doesn't do what I want to do. This is why I have asked this question. \$\endgroup\$
    – VansFannel
    May 24, 2021 at 13:33

1 Answer 1

1
\$\begingroup\$

You could do this with physics though I'm thinking that for what you're trying to achieve, it will be a very complicated solution.

The simplest will be to calculate how long you want it to take for the paddle to get up to full speed, multiply this time by the Delta Time (Tick) of the paddle, then divide the top speed by this value.

float ChangeThisTick = PaddleTopSpeed / (TimeToTopSpeed * GetWorld()->GetDeltaSeconds());

You'll need a second, global variable to keep track of the current multiplier to be added or subtracted depending on whether the AxisValue is positive or negative.

When the AxisValue is zero, you can then check whether the current value of the global variable is positive or negative so you can then reduce or increase the value of this accordingly to slow the paddle down, using a multiplier if you want it to slow down faster than it speeds up.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .