Prevent actor bounce on collision

Hi all,

I’m new to UE4. Currently, I’m trying to make a simple breakout game. I created the paddle as an Actor in the game. The paddle is controlled by the left and right arrows. I’m doing most of the game logic and input handling in C++. The problem I currently have is when the paddle hits one of the side walls, the paddle seems to bounce back and continue moving in the direction it bounced even though I’m not holding down an arrow key.

The paddle has a cube component which simulates physics with a mass of 0. Simulation Generates Hit is checked since I want to be able to hit the ball and call custom code. The side walls also simulate physics and and have simulation generates hit checked.

The APaddle class has the following code:

APaddle::APaddle()
    : m_paddleSpeed(500.0f)
{
	PrimaryActorTick.bCanEverTick = true;
    m_velocity = FVector(0.0f, 0.0f, 0.0f);
}

void APaddle::BeginPlay()
{
	Super::BeginPlay();

    InputComponent->BindAction("MoveLeft", IE_Pressed, this, &APaddle::LeftKeyPressed);
    InputComponent->BindAction("MoveLeft", IE_Released, this, &APaddle::DirectionKeyReleased);
    InputComponent->BindAction("MoveRight", IE_Pressed, this, &APaddle::RightKeyPressed);
    InputComponent->BindAction("MoveRight", IE_Released, this, &APaddle::DirectionKeyReleased);
}

void APaddle::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

    FVector NewLocation = GetActorLocation();
    NewLocation += m_velocity * DeltaTime;
    SetActorLocation(NewLocation);
}

void APaddle::LeftKeyPressed()
{
    m_velocity.X = m_paddleSpeed;
}

void APaddle::RightKeyPressed()
{
    m_velocity.X = -m_paddleSpeed;
}

void APaddle::DirectionKeyReleased()
{
    m_velocity.X = 0.0f;
}

When the paddle hits a wall and I release a key, the m_velocity is 0. I don’t understand why the paddle continues to move. Any help is greatly appreciated. Thanks!

Thanks for the suggestions! I changed to use a Pawn and used an axis for input; it made the code simpler. I still have the same problem with collisions though. Any other ideas?

The first thing I would suggest is, since it is receiving input, to make your Paddle class inherit from Pawn instead of Actor. Pawns are Actors that are meant to be “possessed” and receive input from controllers (or KBM).

To fix your problem with input, though, I would suggest making your MoveRight input action an axis where your inputs to move left have a scale of negative one. Using an axis mapping over an action mapping will allow you to directly set your “m_velocity” X value to the input value as the callback is called even when the axis value is 0.

I’ve just created a small project in an attempt to repro this, and it seems like the bouncing issue may be stemming from having physics being simulated on your paddle.

Thanks again. I found that if I disable physics then the bouncing doesn’t occur, but the paddle can pass through (and slightly move) the wall which is problematic. In short, I just want the wall to cause the paddle to stop moving. Any idea what would cause the paddle to pass through the wall?

Your paddle is moving through your wall because you’re directly setting its location with SetActorLocation so it’s not taking collisions into account. What you can do in your Tick method is use UWorld::LineTraceSingleByProfile to see if there would be a collision (i.e. if the X value of “MyHitResult.Location” is less than your paddle’s location plus/minus half of your paddle’s width plus your velocity’s X value). If there was a collision that close, then you would only move up to the wall. If not, then you would be safe to use your full velocity movement.

EDIT: I would also only suggest performing the line trace if your velocity’s X value is not zero. It can be an expensive operation, so you wouldn’t want to use it even in frames when you’re not moving.

Not a problem! I’m glad I could help you solve your problem and learn a bit more about the engine :slight_smile:

Thanks a ton! I got it to work. I seriously appreciate the time you took to help me.